🔍 Binary Search #4
Replies: 4 comments
Lower Bound : first index i with a[i] ≥ xIf a[m] < x, move left up: l = m + 1 Upper Bound : first index i with a[i] > xIf a[m] ≤ x, move left up: l = m + 1 |
Search in Rotated ArrayNo matter m, one side will be sorted just need to find which one to check if target is inside. Find sorted side :if A[m] >= A[l] : Check if target inside sorted sectionIf Left shortedif A[l] <= target < A[m] : If Right shortedif A[m] < target <= A[r] : With duplicates :
|
Binary Search :Ceiling division (ex: ceil(10,3) = 4 since need at least 4 time to be more than 10)We add k−1 to p to do ceiling division with integers: hours per pile = ceil(p/k) = (p + k − 1) // k, since // is floor and you can’t use partial hours. |




Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Summary
Patterns
Choose
Details
✅ Basic Binary Search
Summary (Key points):
Details
Concept recap:
Generic template (exact index or -1):
Case 1: Binary Search
Case 2: Sqrt(x)
Case 3: First Bad Version
Case 4: Valid Perfect Square
Pitfalls and tips:
Complexity summary:
✅ Lower / Upper Bound
Summary (Key points):
Details
Generic templates:
Case 1: Find First and Last Position of Element in Sorted Array
Case 2: Successful Pairs of Spells and Potions
Case 3: Find Smallest Letter Greater Than Target
Case 4 (bonus technique): Longest Increasing Subsequence via patience sorting
Pitfalls and tips:
Complexity summary:
✅ Search in Rotated Array
Summary (Key points):
Details
Template (no duplicates, strictly increasing before rotation):
Case 1: Search in Rotated Sorted Array
Case 2: Search in Rotated Sorted Array II
Pitfalls and tips:
Complexity summary:
✅ Binary Search on Answer
Summary (Key points):
Details
Generic template (first true in [lo, hi]):
Case 1: Koko Eating Bananas
Case 2: Capacity To Ship Packages Within D Days
Case 3: Find the Smallest Divisor Given a Threshold
Case 4: Minimum Time to Complete Trips
Case 5: Split Array Largest Sum
Pitfalls and tips:
Complexity summary:
All reactions