Converge two pointers onto 11 + 21, slide a window with one add and one drop, and pocket the complexity cheat-sheet.
The big picture
The revision toolkit
The final lap: five pointer patterns that turn brute force into linear time, each with a complexity price tag you can recite.
1
Meet in the middle
Two pointers start at the ends: 2 + 26 = 28 is short of 32, so left moves; 8 + 26 = 34 overshoots, so right retreats. Four moves, one hit.
2
The sliding window
A fixed window never re-sums: 7 − 4 + 6 = 9. Every element enters once and leaves once — O(n) instead of O(n·k).
3
Kadane’s streak
current = max(x, current + x): on [−2, 3, −1, 4, −5] the streak rides the dip and cashes out 3 − 1 + 4 = 6.
4
Write in place
slow marks where the next unique belongs; fast scans. [1, 1, 2, 3, 3, 4] becomes [1, 2, 3, 4] with no second list.
5
Bit-level shortcuts
x ^ x = 0 erases duplicates; n & (n − 1) == 0 singles out powers of two — 8 & 7 is 0, 13 & 12 is 12.
6
Revision = pattern naming
Five shapes cover most problems: converge, slide, scan, rewrite, cancel. Name the shape and the complexity comes free.
The revision toolkit
Meet in the middle1Two pointers start at the ends: 2 + 26 = 28 is short of 32, so left moves; 8 + 26 = 34 overshoots, so right retreats. Four moves, one hit.
The sliding window2A fixed window never re-sums: 7 − 4 + 6 = 9. Every element enters once and leaves once — O(n) instead of O(n·k).
Kadane’s streak3current = max(x, current + x): on [−2, 3, −1, 4, −5] the streak rides the dip and cashes out 3 − 1 + 4 = 6.
Write in place4slow marks where the next unique belongs; fast scans. [1, 1, 2, 3, 3, 4] becomes [1, 2, 3, 4] with no second list.
Bit-level shortcuts5x ^ x = 0 erases duplicates; n & (n − 1) == 0 singles out powers of two — 8 & 7 is 0, 13 & 12 is 12.
Revision = pattern naming6Five shapes cover most problems: converge, slide, scan, rewrite, cancel. Name the shape and the complexity comes free.
6 building blocks make up The revision toolkit
Algorithm in motion
Pointers converge
Sorted data, one target: 32. left and right squeeze inward, retiring one end per comparison.
2
0
left
5
1
8
2
11
3
14
4
18
5
21
6
26
7
right
1 / 11
A sorted array and a target of 32. left sits on 2, right on 26 — we test the two ends and squeeze inward.
How it works
How a window slides
One 3-hour window over five hours of downloads — two arithmetic ops per slide, never a re-sum.
Watch it run
Walkthrough
The fixed-window slide and Kadane’s streak — the two linear scans every revision list starts with.
window_max.py
1downloads =[4,2,1,6,5]
2k =3
3
4window =sum(downloads[:k])
5best = window
6
7for i inrange(k,len(downloads)):
8 window += downloads[i]- downloads[i - k]
9 best =max(best, window)
10
11print("best 3-hour total:", best)
module
Five hourly download totals in GB.
Memory
downloads= [4, 2, 1, 6, 5]
Console
output appears as the program runs…
Step 1/11
Algorithm in motion
Write in place
The second two-pointer shape: slow writes, fast scans — duplicates overwritten, no extra list.
1
0
slow
1
1
fast
2
2
3
3
3
4
4
5
1 / 10
A sorted list with duplicates. slow guards the last unique slot — a[0] = 1 is unique by definition — while fast hunts the next new value.
Challenge
Predict the output
The ledger should hold rolls 1-5 — one went missing. XOR will name it.
predict.py
1rolls =[1,2,4,5]
2x =0
3
4for v in rolls:
5 x ^= v
6
7for v inrange(1,6):
8 x ^= v
9
10print(x)
Your predicted output — one line per console line
Hint: x ^ x = 0 and XOR never cares about order — every value present in both passes cancels itself, leaving only the missing roll.
Practice
Complexity cheat-sheet
Click a pattern, then click its price tag to lock the pair.
8 conceptsTap a card to expand
?Two pointers convergedrops into place
4+6=9
?Kadane scandrops into place
?Brute-force pairsdrops into place
?Sort first, then scandrops into place
?Slow/fast rewritedrops into place
?XOR for missingdrops into place
?n & (n − 1)drops into place
Recap
Key takeaways
Key takeaways6 takeaways
01
Converging pointers retire one end per comparison — sorted input turns O(n²) pair hunting into O(n).
02
A fixed window slides with one subtraction and one addition — never re-sum; that is the O(n) trick.
03
Kadane: current=max(x, current + x). A streak that dips below a fresh start is dropped on the spot.
04
slow/fast rewrites deduplicate in place — O(n) time, O(1) extra space.
05
Two bit tricks to remember: x^ x=0, and n& (n − 1)==0 exactly for powers of two.
06
In the exam: name the pattern first — converge, slide, scan, rewrite, cancel — then the complexity follows.