Bisect in a Loop
An outer loop walks items or rows; each step bisects a sorted structure. The bisect is the same primitive from Foundations; the loop around it is the problem.
Cost: n log m
n outer steps times one O(log m) bisect each. Sorting the searched side first adds O(m log m) once.
When two pointers beat it
If both sides are sorted and the partner only moves one way as x grows, two pointers do the same job in O(n + m). Bisect wins when one side is much smaller, or when the searched side is not walked in order.
Find the partner
Loop x, bisect for target - x, 2x, c - a * a, or the last b[j] >= a[i].
167. Two Sum II - Input Array Is Sorted
167. Two Sum II - Input Array Is Sorted
Fix One, Bisect Another
16. 3Sum Closest
16. 3Sum Closest
Sort + Fix Two, Bisect the Third
Every approach
| Approach | Time | When an interviewer wants it |
|---|---|---|
| Aux space | ||
| Brute Force | O(n3) | opening line |
| O(1) | ||
| Sort + Fix Two, Bisect the Third | O(n2 log n) | "without two pointers" |
| O(sort) | ||
| Sort + 2 Converging Pointers | O(n2) | the expected answer |
| O(sort) | ||
| Hash map | does not work | "why no hash version?": hashing answers exact membership, not nearest |
| - |
1099. Two Sum Less Than K
1099. Two Sum Less Than K
Given an array nums of integers and an integer k, return the maximum sum such that there exists i < j with nums[i] + nums[j] = sum and sum < k. If no i, j exist satisfying this equation, return -1.
nums = [34, 23, 1, 24, 75, 33, 54, 8],k = 60returns58:34 + 24.nums = [10, 20, 30],k = 15returns-1: no pair sums below 15.- Constraints:
1 <= nums.length <= 100,1 <= nums[i] <= 1000,1 <= k <= 2000.
Brute Force
Every approach
| Approach | Time | When an interviewer wants it |
|---|---|---|
| Aux space | ||
| Brute Force | O(n2) | opening line |
| O(1) | ||
| Sort + Fix One, Bisect Another | O(n log n) | "without two pointers" |
| O(sort) | ||
| Sort + 2 Converging Pointers | O(n log n) | the expected answer |
| O(sort) | ||
| Count by Value | O(n + D2) | when the small value range is the point |
| O(D) |
259. 3Sum Smaller
259. 3Sum Smaller
Brute Force
Every approach
| Approach | Time | When an interviewer wants it |
|---|---|---|
| Aux space | ||
| Brute Force | O(n3) | opening line |
| O(1) | ||
| Sort + Fix Two, Bisect the Third | O(n2 log n) | "without two pointers" |
| O(sort) | ||
| Sort + 2 Converging Pointers | O(n2) | the expected answer, and the common follow-up to 3Sum |
| O(sort) |
611. Valid Triangle Number
611. Valid Triangle Number
Brute Force
Every approach
| Approach | Time | When an interviewer wants it |
|---|---|---|
| Aux space | ||
| Brute Force | O(n3) | opening line |
| O(sort) | ||
| Sort + Fix Two, Bisect the Third | O(n2 log n) | "without two pointers" |
| O(sort) | ||
| Sort + Fix Largest + 2 Converging Pointers | O(n2) | the expected answer |
| O(sort) |
18. 4Sum
18. 4Sum
Brute Force
Every approach
| Approach | Time | When an interviewer wants it |
|---|---|---|
| Aux space | ||
| Brute Force | O(n4) | opening line |
| O(Q) | ||
| Fix Two + 1-Pass Index Map | O(n3) | when the input can't be sorted |
| O(n + Q) | ||
| Sort + Fix Three, Bisect the Fourth | O(n3 log n) | "without two pointers" |
| O(sort + Q) | ||
| Sort + Fix Two + 2 Converging Pointers | O(n3) | the expected answer |
| O(sort) | ||
| Recursive kSum | O(nk-1) | "how would you do 5Sum?" |
| O(k + sort) |
1346. Check If N and Its Double Exist
1346. Check If N and Its Double Exist
Check Every Pair
1385. Find the Distance Value Between Two Arrays
1385. Find the Distance Value Between Two Arrays
Check Every Pair
2154. Keep Multiplying Found Values by Two
2154. Keep Multiplying Found Values by Two
Sort then Bisect
633. Sum of Square Numbers
633. Sum of Square Numbers
Enumerate a, Test the Complement
1855. Maximum Distance Between a Pair of Values
1855. Maximum Distance Between a Pair of Values
Without Reversing
Common elements
Walk the first array (or row) and bisect every other one for the same value.
349. Intersection of Two Arrays
349. Intersection of Two Arrays
Bisect Each Element
350. Intersection of Two Arrays II
350. Intersection of Two Arrays II
Bisect and Consume
1198. Find Smallest Common Element in All Rows
1198. Find Smallest Common Element in All Rows
Probe Every Other Row
1213. Intersection of Three Sorted Arrays
1213. Intersection of Three Sorted Arrays
Probe Every Other Array
2540. Minimum Common Value
2540. Minimum Common Value
Bisect Each Element
Row by row
Each row is sorted on its own, so bisect each row for its boundary.
1351. Count Negative Numbers in a Sorted Matrix
1351. Count Negative Numbers in a Sorted Matrix
Per-Row Upper-Bound (Manual Binary Search)
1337. The K Weakest Rows in a Matrix
1337. The K Weakest Rows in a Matrix
Count Soldiers, Keep K with a Heap
1428. Leftmost Column with at Least a One
1428. Leftmost Column with at Least a One
Custom Bisect per Row
Keep a sorted tail
Bisect into an array you are building as you go.