Two-Pointer Optimization Pattern
The Two-Pointer technique optimizes search boundaries by managing two independent index references that move toward each other or in tandem, bypassing the need for nested O(n^2) loops.
💡 Core Recognition Strategy
- Input State: The linear data structure (array or arraylist) is sorted or organized sequentially.
- Objective: Locating a pair, triplet, or subarray that satisfies a strict target evaluation.
💻 Java Implementation: Two Sum II (Sorted Input)
public int[] twoSumSorted(int[] numbers, int target) {
int left = 0; // #1
int right = numbers.length - 1; // #2
while (left < right) {
int currentSum = numbers[left] + numbers[right];
if (currentSum == target) {
return new int[]{left + 1, right + 1}; // #3
} else if (currentSum < target) {
left++; // #4
} else {
right--; // #5
}
}
return new int[]{-1, -1};
}
- Initialize the left pointer reference at the absolute beginning of the sorted collection (index 0).
- Initialize the right pointer reference at the final terminal index position of the array dataset.
- The problem constraints dictate a 1-indexed return array output, so we offset our 0-indexed positions by adding
1. - If our combined value falls short of the target value, we increment
leftto pull a larger integer layer into the computation. - If our combined value exceeds the target value, we decrement
rightto bring a smaller value scale into the calculation.