Patterns

Most array problems are one of a handful of moves. Learn each move once and you will start to spot it everywhere. Each one has a live visualizer running the real code.

  1. 01Two pointersWalk in from both ends of a sorted array to find a pair with a given sum.O(n)
  2. 02Three-way partitionSplit an array into less than, equal to and greater than a pivot in one pass.O(n)
  3. 03Sliding windowSlide a window of k items, adding one and dropping one, instead of re-summing.O(n)
  4. 04Variable sliding windowGrow the window until it breaks a rule, then shrink it from the left.O(n)
  5. 05Prefix sumsKeep running totals so any subarray sum is a single subtraction.O(n)
  6. 06Monotonic stackKeep a stack in order to find every next greater value in one pass.O(n)