Find the first index where something breaks. The question rarely says "sorted", but a value built from the index and a[i] only ever flips one way.
Build g(i)
Name the thing that changes at the answer and write it as a function of i:
Problem g(i) flips when Fixed Point a[i] - iit reaches 0 Missing count a[i] - a[0] - iit reaches k H-Index n - i vs a[i]the curves cross Single element is i the start of a pair pairs start on odd indices
Why g is monotone
Each g compares something that rises by at least one per step against something that rises by exactly one, or falls. Once it crosses, it cannot cross back, so False ... False True ... True holds and the lower-bound loop applies.
Template: lambda + lower bound
g = lambda i: ... # one line
lo, hi = 0, len(a) - 1
while lo < hi:
mid = lo + (hi - lo) // 2
if not_yet(g(mid)):
lo = mid + 1
else:
hi = mid
Verify the survivor
The loop always stops on one index, even when no index works. Check lo before returning it (a[lo] == lo, citations[lo] >= cited(lo)), and return the fallback otherwise.
Black-box predicate
The judge hands you g as an API call.
278. First Bad Version
FIG. 278 FIRST BAD VERSION ● INTERACTIVE
visualization loads as you reach it
class Solution:
def firstBadVersion(self, n: int) -> int:
# Answers [1, n]: version 0 is never bad, and n is guaranteed bad so hi is safe
lo, hi = 0, n
while lo < hi:
mid = lo + (hi - lo) // 2
if isBadVersion(mid) is False:
lo = mid + 1
else:
hi = mid
return lo
How it worksO(log N) · O(1)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - top ↑
374. Guess Number Higher or Lower
374. Guess Number Higher or Lower LeetCode FIG. 374 GUESS NUMBER HIGHER OR LOWER ● INTERACTIVE
visualization loads as you reach it
class Solution:
def guessNumber(self, n: int) -> int:
# Answers [1, n]: the pick is in 1..n, so guess(0) says higher and lo moves up
lo, hi = 0, n
while lo < hi:
mid = lo + (hi - lo) // 2
if guess(mid) == 1: # 1 if guess is lower than the target
lo = mid + 1
else: # -1 or 0 if guess is greater than or equal to the target
hi = mid
return lo
How it worksO(log N) · O(1)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - top ↑
1533. Find the Index of the Large Integer
1533. Find the Index of the Large Integer LeetCode
Compare Equal-Length Halves
FIG. 1533 FIND THE INDEX OF THE LARGE INTEGER ● INTERACTIVE
visualization loads as you reach it
class Solution:
def getIndex(self, reader: "ArrayReader") -> int:
# Answers [0, n): the large integer is always a real index, so no post-check
lo, hi = 0, reader.length() - 1
while lo < hi:
mid = lo + (hi - lo) // 2
if (hi - lo) % 2 == 1:
compared = reader.compareSub(lo, mid, mid + 1, hi)
else:
compared = reader.compareSub(lo, mid, mid, hi)
if compared == -1:
lo = mid + 1
else:
hi = mid
return lo
How it worksO(log N) · O(1)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - top ↑
a[i] vs i
Values stay pinned to their index until something slips.
1064. Fixed Point
Lower-Bound on arr[i] - i
FIG. 1064 FIXED POINT ● INTERACTIVE
visualization loads as you reach it
class Solution:
def fixedPoint(self, arr: List[int]) -> int:
# Answers [0, len(arr)): a real index, so post-check arr[lo] == lo
lo, hi = 0, len(arr) - 1
while lo < hi:
mid = lo + (hi - lo) // 2
if arr[mid] < mid:
lo = mid + 1
else:
hi = mid
return lo if arr[lo] == lo else -1
How it worksO(log N) · O(1)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - top ↑
268. Missing Number
time space
XOR Indices & Values↗ XOR O(N) time best time O(1) aux best space Bitmask + log2↗ XOR O(N) time best time O(1) aux best space Sum Formula↗ Array & String O(N) time best time O(1) aux best space Sort, then Find the First Gap O(log N + N log N) time O(sort) aux
Sort, then Find the First Gap
FIG. 268 MISSING NUMBER 2 ● INTERACTIVE
visualization loads as you reach it
class Solution:
def missingNumber(self, nums: List[int]) -> int:
nums.sort()
lo, hi = 0, len(nums)
while lo < hi:
mid = lo + (hi - lo) // 2
if mid >= nums[mid]:
lo = mid + 1
else:
hi = mid
return lo
How it worksO(log N + N log N) · O(sort)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - top ↑
287. Find the Duplicate Number
Lower-Bound on the Sorted Array
FIG. 287 FIND THE DUPLICATE NUMBER ● INTERACTIVE
visualization loads as you reach it
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
nums = sorted(nums)
expected = lambda i: i + 1
# Answers [0, len(nums)): the last slot always mismatches, so lo is a real index
lo, hi = 0, len(nums)
while lo < hi:
mid = lo + (hi - lo) // 2
if expected(mid) == nums[mid]:
lo = mid + 1
else:
hi = mid
return nums[lo]
How it worksO(log N + N log N) · O(sort + N)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - top ↑
Missing count
missing(i) counts the gaps before index i and only grows.
1539. Kth Missing Positive Number
1539. Kth Missing Positive Number LeetCode FIG. 1539 KTH MISSING POSITIVE NUMBER ● INTERACTIVE
visualization loads as you reach it
class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
missing = lambda i: arr[i] - 1 - i # Return the number of missing numbers
# Answers [0, len(arr)]: lo can be either end, lo - 1 steps back to the last slot below k
lo, hi = 0, len(arr)
# find first greatest `missing` that is <= k
while lo < hi:
mid = lo + (hi - lo) // 2
if missing(mid) < k:
lo = mid + 1
else:
hi = mid
return arr[lo - 1] + (k - missing(lo - 1))
How it worksO(log N) · O(1)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - top ↑
1060. Missing Element in Sorted Array
1060. Missing Element in Sorted Array LeetCode FIG. 1060 MISSING ELEMENT IN SORTED ARRAY ● INTERACTIVE
visualization loads as you reach it
class Solution:
def missingElement(self, nums: List[int], k: int) -> int:
missing = lambda i: nums[i] - nums[0] - i # Return the number of missing numbers
# Answers [1, len(nums)]: missing(0) is 0 < k, so lo steps back to a real index via lo - 1
lo, hi = 0, len(nums)
# find first greatest `missing` that is <= k
while lo < hi:
mid = lo + (hi - lo) // 2
if missing(mid) < k:
lo = mid + 1
else:
hi = mid
return nums[lo - 1] + (k - missing(lo - 1))
How it worksO(log N) · O(1)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - top ↑
Crossing curves
n - i falls while a[i] rises; the answer is where they cross.
275. H-Index II
Boundary on the Citation Curve
FIG. 275 H INDEX II ● INTERACTIVE
visualization loads as you reach it
class Solution:
def hIndex(self, citations: List[int]) -> int:
cited = lambda i: len(citations) - i
# Answers [0, len(citations)): a real index, so post-check citations[lo] >= cited(lo)
lo, hi = 0, len(citations) - 1
while lo < hi:
mid = lo + (hi - lo) // 2
if cited(mid) > citations[mid]:
lo = mid + 1
else:
hi = mid
return cited(lo) if citations[lo] >= cited(lo) else 0
How it worksO(log N) · O(1)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - top ↑
274. H-Index
Sort, then Boundary Search
FIG. 274 H INDEX ● INTERACTIVE
visualization loads as you reach it
class Solution:
def hIndex(self, citations: List[int]) -> int:
citations = sorted(citations)
cited = lambda i: len(citations) - i
# Answers [0, len(citations)): a real index, so post-check citations[lo] >= cited(lo)
lo, hi = 0, len(citations) - 1
while lo < hi:
mid = lo + (hi - lo) // 2
if cited(mid) > citations[mid]:
lo = mid + 1
else:
hi = mid
return cited(lo) if citations[lo] >= cited(lo) else 0
How it worksO(log N + N log N) · O(sort + N)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - top ↑
Pair parity
Before the single element, each pair starts on an even index; after it, on an odd one.
540. Single Element in a Sorted Array
540. Single Element in a Sorted Array LeetCode time space
XOR Fold O(N) time O(1) aux
Parity of the Right Half O(log N) time best time O(1) aux FIG. 540 SINGLE ELEMENT IN A SORTED ARRAY ● INTERACTIVE
visualization loads as you reach it
class Solution:
def singleNonDuplicate(self, nums: List[int]) -> int:
xor = 0
for number in nums:
xor = xor ^ number
return xor
How it worksO(N) · O(1)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - top ↑