Each subpattern solves a specific type of problem. Click on a card to view its template.
Exact match on a sorted array — return the index or -1.
Locate the first or last index where a condition flips from false to true.
Determine which half is sorted, then decide which side to discard.
The answer lies in a monotone range — binary search on the value, validate with a greedy check.
Map a 2D matrix to a virtual 1D sorted array and run classic binary search.
Binary search on a continuous or implicit domain — no explicit array needed.
Template for: Classic Binary Search
1// Classic binary search — sorted array, exact match2int binarySearch(vector<int>& arr, int target)3{4 int lo = 0;5 int hi = (int)arr.size() - 1;67 while (lo <= hi)8 {9 int mid = lo + (hi - lo) / 2;1011 if (arr[mid] == target)12 {13 return mid; // found14 }15 else if (arr[mid] < target)16 {17 lo = mid + 1; // search right half18 }19 else20 {21 hi = mid - 1; // search left half22 }23 }2425 return -1; // not found (return lo for insertion index)26}
LeetCode #704
LeetCode #35
LeetCode #69
LeetCode #374
LeetCode #34
LeetCode #278
LeetCode #852
LeetCode #153
LeetCode #33
LeetCode #81
LeetCode #154
LeetCode #162
LeetCode #875
LeetCode #1011
LeetCode #1482
LeetCode #410
LeetCode #1552
LeetCode #74
LeetCode #240
LeetCode #378
LeetCode #1351
LeetCode #4
LeetCode #719
LeetCode #778
LeetCode #878