Advanced Problem Solving and Revision

Lesson 20 of 20

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.

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.

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).

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.

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.

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.

Revision = pattern naming

Five shapes cover most problems: converge, slide, scan, rewrite, cancel. Name the shape and the complexity comes free.