Comprehensive Guide for Solving Problems with Sorted Arrays in DSA
4 min readSep 30, 2024
cheat sheet to solve Sorted Array DSA problems
1. Binary Search
- Problem Type: Searching an element in a sorted array.
- Approach: Divide and conquer; narrow down the search range by comparing the middle element.
- Time Complexity: O(log n)
Steps:
- Set
low = 0
,high = n - 1
. - Calculate
mid = low + (high - low) / 2
. - If
arr[mid] == target
, returnmid
. - If
arr[mid] > target
, search the left half. - If
arr[mid] < target
, search the right half.
Key Use Cases:
- Search element in a sorted array.
- Finding the first/last occurrence of an element.
2. Two Pointers Approach
- Problem Type: Find pairs or triplets that satisfy a condition (e.g., sum, difference).
- Approach: Use two pointers, one starting from the beginning and one from the end, and adjust based on conditions.
- Time Complexity: O(n)
Steps:
- Initialize two pointers
i = 0
(start) andj = n - 1
(end). - Calculate
sum = arr[i] + arr[j]
. - If
sum == target
, return the pair. - If
sum > target
, decrementj
(move the right pointer left). - If
sum < target
, incrementi
(move the left pointer right).
Key Use Cases:
- Two Sum problem.
- Three Sum problem (with a nested loop for third element).
- Find if there are two numbers with a given difference.
3. Rotated Sorted Array
- Problem Type: Search in a rotated sorted array.
- Approach: A variation of binary search that accounts for the rotation.
- Time Complexity: O(log n)
Steps:
- Perform binary search, but decide whether to search in the left or right half by checking which half is sorted.
- If the left half is sorted, check if the target lies in that range.
- Otherwise, search the right half.
Key Use Cases:
- Find the minimum element in a rotated sorted array.
- Search for a target in a rotated sorted array.
4. Find the Peak Element
- Problem Type: Find a local peak in a sorted or unsorted array.
- Approach: Use binary search to locate the peak element.
- Time Complexity: O(log n)
Steps:
- If
arr[mid]
is greater than its neighbors, it's the peak. - If
arr[mid] < arr[mid + 1]
, search the right half. - Else, search the left half.
Key Use Cases:
- Find peak in a mountain array.
- Local maxima in a strictly increasing or decreasing array.
5. Find First and Last Occurrence
- Problem Type: Find the first and last positions of a given element in a sorted array.
- Approach: Modify binary search to find the boundaries.
- Time Complexity: O(log n)
Steps:
- To find the first occurrence, search while
arr[mid] == target
andmid == 0
orarr[mid - 1] != target
. - To find the last occurrence, search while
arr[mid] == target
andmid == n - 1
orarr[mid + 1] != target
.
6. Find Minimum or Maximum in a Rotated Sorted Array
- Problem Type: Find the minimum or maximum element in a rotated sorted array.
- Approach: Binary search for the point of inflection.
- Time Complexity: O(log n)
Steps:
- If
arr[low] <= arr[high]
, returnarr[low]
(already sorted). - Otherwise, find the smallest element in the unsorted part using binary search.
7. Merge Two Sorted Arrays
- Problem Type: Merging two sorted arrays into a single sorted array.
- Approach: Use two pointers to compare elements from both arrays.
- Time Complexity: O(n + m)
Steps:
- Use two pointers
i
andj
to compare elements fromarr1
andarr2
. - Insert the smaller element into the result array and move the pointer.
- When one array is exhausted, add remaining elements from the other array.
8. Find the Intersection of Two Sorted Arrays
- Problem Type: Find common elements between two sorted arrays.
- Approach: Use two pointers, one for each array.
- Time Complexity: O(n + m)
Steps:
- Initialize pointers
i
andj
. - Compare
arr1[i]
andarr2[j]
. - If
arr1[i] == arr2[j]
, add it to the result and increment both pointers. - If
arr1[i] < arr2[j]
, incrementi
. - If
arr1[i] > arr2[j]
, incrementj
.
9. Remove Duplicates in a Sorted Array
- Problem Type: Remove duplicates and return the new length.
- Approach: Use two pointers to overwrite duplicates.
- Time Complexity: O(n)
Steps:
- Use two pointers
i
andj
wherei
tracks the non-duplicate elements. - If
arr[j] != arr[i]
, incrementi
and copyarr[j]
toarr[i]
. - Return
i + 1
as the new length.
10. Sliding Window for Fixed or Variable Size Problems
- Problem Type: Sum or product of subarrays in a sorted array.
- Approach: Use a sliding window to find sums or other operations efficiently.
- Time Complexity: O(n)
Steps:
- Initialize a window with the first
k
elements. - Slide the window by removing the element going out of the window and adding the next element.
- Track the sum (or product) for each window.
11. Median of Two Sorted Arrays
- Problem Type: Find the median of two sorted arrays.
- Approach: Use binary search on the smaller array to partition both arrays.
- Time Complexity: O(log(min(n, m)))
Steps:
- Perform binary search to partition both arrays.
- Ensure that the left half contains smaller elements than the right half.
- Calculate the median based on partition indices.