PROBLEM SOLVING TRICKS - ARRAYS
1-Dimensional Arrays Approaches
1. Two-Pointer Technique
Use Case: When you need to process pairs, remove duplicates, or find subarrays.
Approach: Use two pointers (start & end, or slow & fast) to traverse the array efficiently.
Examples:
- Reverse an array: Swap elements from start and end pointers.
- Search an element:
- Use Linear Search for unsorted arrays (simpler and equally efficient).
- Use Two-Pointer (Binary Search) only if the array is sorted (O(log n) time).
- Remove duplicates: Use a slow pointer to track unique elements.
- Two-sum problem: Sort the array and use two pointers to find pairs.
2. Sliding Window Technique
The Sliding Window is an algorithmic technique used to efficiently solve problems involving arrays, strings, or sequences by maintaining a "window" of elements that satisfies certain conditions.
Use Case: For problems involving subarrays with a fixed size or a condition (e.g., maximum sum, smallest subarray).
Approach: Maintain a window (subarray) and adjust its size dynamically.
Examples:
- Maximum sum of k consecutive elements.
- Longest subarray with sum ≤ K.
- Minimum size subarray with sum ≥ target.
3. Binary Search on Arrays
Use Case: When the array is sorted (or can be sorted) and you need O(log n) search.
Approach: Divide the search space in half repeatedly.
Examples:
- Find an element in a sorted array.
- Find the first/last occurrence of an element.
- Find peak element in mountain array.
- Search in rotated sorted array.
4. Cyclic Rotations & Reversals
Use Case: Rotating an array or reversing parts of it.
Approach: Use reversal tricks to rotate in O(1) space.
Example:
- Rotate array right by k steps
- Reverse specific portions of an array
- Implement circular buffer operations
5. Frequency Counting (Hashing)
Use Case: When you need to count occurrences or find duplicates.
Approach: Use a hash map (or an array if elements are bounded) to store frequencies.
Examples:
- Find duplicates in an array.
- First non-repeating element.
- Find all numbers disappeared in an array.
- Group anagrams together.
6. Swap & Reorder Techniques
Use Case: When you need to modify the array in-place (e.g., move zeros to end, segregate even-odd numbers).
Approach: Use a pointer to track the correct position and swap elements.
Examples:
- Move all zeros to the end.
- Segregate even and odd numbers.
- Dutch national flag problem.
- Move all negatives to one side.
7. Prefix Sum (Cumulative Sum)
Use Case: When you need to compute range sums or differences efficiently.
Approach: Precompute cumulative sums to answer range queries in O(1) time.
Examples:
- Find equilibrium index (where left sum = right sum).
- Subarray sum equals K.
- Range sum queries on immutable arrays.
- Maximum average subarray.
8. Kadane's Algorithm (Maximum Subarray Sum)
Use Case: Finding the maximum sum of a contiguous subarray.
Approach: Keep track of the current sum and reset it if it becomes negative.
Variations:
- Maximum subarray sum (classic problem)
- Maximum product subarray
- Maximum circular subarray sum
- Maximum sum with at most k elements