Binary Search
Halve the search space each step — on sorted arrays, and on the answer itself.
This topic is part of Pro.
Free covers the C and Python courses, the first three DSA topics, the opening system design lessons and the beginner AI lessons, all with the full coach. Pro opens the rest of the library in the FollowtheDamnCode app.
What is in it
- 1Classic binary searchFind a target in a sorted array by comparing with the middle and discarding half, keeping a clear invariant. Pro
- 2Finding boundariesFirst position where a condition becomes true — lower bound, upper bound, first and last occurrence. Pro
- 3Rotated sorted arraysOne half of a rotated array is always sorted — check which, and whether the target lies in it. Pro
- 4Binary search on the answerWhen "can it be done with x?" flips from no to yes as x grows, binary search x itself. Pro