โ† Competitive Programming learning path
Competitive Programming ยท Module 4 of 10

Sorting, searching and two pointers

Classic tools that turn O(n^2) ideas into O(n log n) or O(n).

๐Ÿ“– Key ideas

  1. Sort first: many problems become simple on sorted data.
  2. Binary search finds a value or a boundary in O(log n).
  3. Binary search on the answer: test "is x feasible?" and halve the range.
  4. Two pointers move through a sorted array from both ends or together.
  5. Sliding window keeps a running range for subarray problems.
๐Ÿ’ก Real-life example: Two pointers are two people searching a sorted bookshelf from both ends until they meet.
Binary searchHalve the search range each step
Two pointersTwo indexes moving through an array
Sliding windowMoving range over a subarray
lower_boundFirst position not less than a value