DSA Array Patterns
1. Sliding Window
Ideal for problems involving subarrays with a fixed or variable size, such as maximum sum subarray
of size k.
2. Two Pointers
Useful in sorted arrays for finding pairs with a specific sum or rearranging elements.
3. Prefix Sum
Helps to compute range sums quickly. Commonly used in problems involving frequent range
queries.
4. Kadane's Algorithm
Efficient algorithm to find the maximum sum subarray in O(n) time.
5. Binary Search
Used to find elements or positions in sorted arrays. Can be extended to search boundaries or peak
elements.
6. Cyclic Sort
Used when the input array contains numbers in a fixed range (e.g., 1 to n). Efficiently sorts with O(1)
space.
7. Dutch National Flag
Useful for sorting arrays with three distinct values, e.g., sorting 0s, 1s, and 2s.
8. Merge Intervals (on Arrays)
Involves sorting and then merging overlapping intervals. Common in meeting schedule problems.
9. Monotonic Stack
Helps solve problems like Next Greater Element, Largest Rectangle in Histogram, etc.
10. Subarray Sum Equals K
Involves using a HashMap to store prefix sums and quickly find the number of subarrays with a
given sum.