Secondary Loop BS
Binary search as the inner step of an outer loop. The outer pass walks elements (or rows); for each one we bisect into a sorted structure. Total cost is O(N log M) - the loop times the search.
Bisect Derivatives
Loop one value, binary-search for its partner (complement, double, or matching boundary).
167. Two Sum II - Input Array Is Sorted
Medium·
3 Approaches · 3 patterns
Converging Pointers↗ Two Pointers
O(N)O(1)
Lookup Before Insert↗ Hash & Set
O(N)O(N)
Complement by Bisect
O(N log N)O(1)
FIG. 167 TWO SUM II INPUT ARRAY IS SORTED 2● INTERACTIVE
visualization loads as you reach it
1855. Maximum Distance Between a Pair of Values
Medium·
2 Approaches
Without Reversing
O(M log N)O(1)
By Reversing
O(N + M log N)O(1)
FIG. 1855 MAXIMUM DISTANCE BETWEEN A PAIR OF VALUES 1● INTERACTIVE
visualization loads as you reach it
1346. Check If N and Its Double Exist
Easy·
Sort then Bisect the Double
O(2 N log N)O(sort)
FIG. 1346 CHECK IF N AND ITS DOUBLE EXIST● INTERACTIVE
visualization loads as you reach it
300. Longest Increasing Subsequence
Medium·
4 Approaches · 2 patterns
LRU Cache↗ Dynamic Programming
O(n^2)O(3n)
Memoization↗ Dynamic Programming
O(n^2)O(2n)
Tabulation, O(n) Space↗ Dynamic Programming
O(n^2)O(n)
Binary Search, O(n log n)
O(n log n)O(n)
FIG. 300 LONGEST INCREASING SUBSEQUENCE BISECT● INTERACTIVE
visualization loads as you reach it
Bisect Derivatives in a 2D Matrix
Each row is independently sorted, so bisect row by row.
1351. Count Negative Numbers in a Sorted Matrix
Easy·
Per-Row Boundary by Bisect
O(N log M)O(1)
FIG. 1351 COUNT NEGATIVE NUMBERS IN A SORTED MATRIX● INTERACTIVE
visualization loads as you reach it
1337. The K Weakest Rows in a Matrix
Easy·
Count Soldiers, Keep K with a Heap
O(N log(M*K))O(2K)
FIG. 1337 THE K WEAKEST ROWS IN A MATRIX● INTERACTIVE
visualization loads as you reach it
Intersection
Iterate the first array (or row), and binary-search every other one for the same value.
349. Intersection of Two Arrays
Easy·
Bisect Each Element
O(N log N + M log M + N log M)O(sort)
FIG. 349 INTERSECTION OF TWO ARRAYS● INTERACTIVE
visualization loads as you reach it
350. Intersection of Two Arrays II
Easy·
Bisect and Consume
O(N log N + m log m + N*m)O(sort)
FIG. 350 INTERSECTION OF TWO ARRAYS II● INTERACTIVE
visualization loads as you reach it
1198. Find Smallest Common Element in All Rows
Medium·
Probe Every Other Row
O(N*M log M)O(1)
FIG. 1198 FIND SMALLEST COMMON ELEMENT IN ALL ROWS● INTERACTIVE
visualization loads as you reach it
1213. Intersection of Three Sorted Arrays
Easy·
Probe Every Other Array
O(2M log M)O(1)
FIG. 1213 INTERSECTION OF THREE SORTED ARRAYS● INTERACTIVE
visualization loads as you reach it
Custom Bisect
When the input is an opaque interface, hand-roll the bisect against its accessor.
1428. Leftmost Column with at Least a One
Medium·
Custom Bisect per Row
O(N log M)O(1)
FIG. 1428 LEFTMOST COLUMN WITH AT LEAST A ONE● INTERACTIVE
visualization loads as you reach it