Advanced Problem Solving and Revision
Lesson 20 of 20Two pointers converge on a hidden pair sum, then a sliding window sweeps the best stretch in a single O(n) pass.
The pattern toolkit
The final lap is a revision of the templates that crack most interview warm-ups: converging pointers, sliding windows, prefix sums and two bit tricks.
Pattern first, code second
The real skill is naming the template within seconds of reading a problem: sorted array → two pointers, contiguous run → sliding window, range sums → prefix.
Two pointers
Start both ends of a SORTED array. Every comparison retires one element forever: 6 comparisons here beat the 28 checks of a nested loop.
Sliding window
Keep a running sum and update it in O(1) per slide: sum += a[i] - a[i-k]. Brute force re-adds k elements for every one of the n-k windows.
Prefix sums
pre[i] = sum of the first i elements. Any range sum collapses to pre[r+1] - pre[l] after one O(n) build — 14 - 4 = 10 for a[2..4].
Bit tricks
n & (n-1) clears the lowest set bit: 12 & 11 = 8. When the result is 0, n was a power of two: 16 & 15 = 0.
Pointers converge
Target 25 hides in a sorted array — left and right squeeze it out in six comparisons.
A sorted array and target 25. left starts at the smallest value (3), right at the biggest (34).
Decision flow
The two-pointer loop: one comparison, one pointer moves, one element retires forever.
Walkthrough
The converging pair hunt and the sliding window sweep — both single-pass, both O(n).
1public class PairSum {2public static void main(String[] args) {3int[] a = {3, 7, 9, 12, 16, 21, 27, 34};4int target = 25;5int l = 0, r = a.length - 1;67while (l < r) {8int sum = a[l] + a[r];9if (sum == target) {10System.out.println(a[l] + " + " + a[r] + " = " + target);11return;12}13if (sum < target) l++;14else r--;15}16System.out.println("no pair");17}18}
a = [3, 7, 9, 12, 16, 21, 27, 34] — sorted ascending, the two-pointer precondition.
Sliding the window
Ten readings, window of 3 — eight O(1) slides and the best stretch wins at position 6.
Ten sensor readings, window size 3 — find the 3-wide stretch with the biggest sum.
Predict the output
A prefix-sum table — two range queries answered by subtraction alone.
1public class Predict {2public static void main(String[] args) {3int[] a = {3, 1, 4, 1, 5};4int[] pre = new int[6];5for (int i = 0; i < 5; i++) {6pre[i + 1] = pre[i] + a[i];7}8System.out.println(pre[2] - pre[0]);9System.out.println(pre[5] - pre[2]);10}11}
Hint: pre[i] holds the sum of the first i elements: [0, 3, 4, 8, 9, 14]. sum of a[l..r] = pre[r+1] - pre[l], so a[0..1] is 4 - 0 and a[2..4] is 14 - 4.
Concept check
Click a term, then click its meaning to lock the pair.
Key takeaways
Two pointers converge on sorted arrays: every comparison retires one element — O(n) instead of O(n²).
Sliding windows reuse the running sum: sum += a[i] - a[i-k] makes each slide O(1).
Prefix sums cost one O(n) build and turn every range query into a single subtraction.
Brian Kernighan’s trick: n & (n-1) clears the lowest set bit — loop it to count set bits.
n > 0 && (n & (n-1)) == 0 is the one-line power-of-two test.
Revision mindset: name the pattern first, write the loop second.