Binary Search Pattern: Template + 254 LeetCode Problems
Halve the search space each step — over an array, or over the answer itself.
- 31 Easy
- 138 Medium
- 85 Hard
- O(log n) time
What the binary search pattern is
Binary search needs a sorted array far less than it needs a monotone predicate — some property that is false for every candidate below a threshold and true for every candidate above it. Once you can write that predicate, you can bisect anything, including a range of possible answers that is nowhere stored in memory: guess a capacity, ask whether the shipment fits, and move the bound. Most binary search bugs are off-by-one errors in the exit condition, and the cure is to stop searching for a value and search for a boundary instead. Keep the loop as `while lo < hi`, never write `mid - 1` on the side you are keeping, and let the loop end when the two bounds meet — the survivor is the smallest input for which the predicate holds. That single shape covers first occurrence, last occurrence, insertion point and minimum feasible answer, so there is only one template to remember and it terminates by construction.
When to use it
- The array is sorted, or rotated-sorted, and you want a position rather than a scan.
- The answer is a number in a known range and checking a candidate is cheaper than finding it.
- The phrase "minimum largest", "maximum smallest", or "minimise the maximum" appears in the statement.
- A brute force over the answer range would work but is one order too slow.
The binary search template in Python
The shape, not a solution to any one problem. Adapt the condition and the summary being maintained; the skeleton stays the same across the 254 problems listed below.
def smallest_feasible(low, high, feasible):
"""Smallest x in [low, high] with feasible(x) true, assuming it is monotone."""
while low < high:
mid = low + (high - low) // 2 # floor: biases toward low, never overflows
if feasible(mid):
high = mid # mid works, so the answer is mid or lower
else:
low = mid + 1 # mid fails, so the answer is strictly higher
return lowComplexity characteristics
- Time
- O(log n)
- Auxiliary space
- O(1)
Every iteration discards half of what is left, so the loop runs log₂ n times. When the search is over a range of possible answers rather than over an array, the cost is O(log W) iterations times whatever the feasibility check costs, where W is the width of the range — that product is the figure to quote, because the check is usually a linear scan and the real complexity is O(n log W). The iterative form uses constant space.
All 254 binary search LeetCode problems
Every problem in the library the binary search pattern applies to, grouped by LeetCode's own difficulty rating. 213 of the 254 carry a complete Python solution with a worked example and complexity analysis; the rest are listed for completeness, with the LeetCode Premium ones marked.
Easy (31)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 35 | Search Insert Position | Easy | Array, Binary Search |
| 69 | Sqrt(x) | Easy | Math, Binary Search |
| 222 | Count Complete Tree Nodes | Easy | Bit Manipulation, Tree, Binary Search +1 |
| 268 | Missing Number | Easy | Bit Manipulation, Array, Hash Table +3 |
| 278 | First Bad Version | Easy | Binary Search, Interactive |
| 349 | Intersection of Two Arrays | Easy | Array, Hash Table, Two Pointers +2 |
| 350 | Intersection of Two Arrays II | Easy | Array, Hash Table, Two Pointers +2 |
| 367 | Valid Perfect Square | Easy | Math, Binary Search |
| 374 | Guess Number Higher or Lower | Easy | Binary Search, Interactive |
| 441 | Arranging Coins | Easy | Math, Binary Search |
| 704 | Binary Search | Easy | Array, Binary Search |
| 744 | Find Smallest Letter Greater Than Target | Easy | Array, Binary Search |
| 270 | Closest Binary Search Tree ValuePremium | Easy | Tree, Depth-First Search, Binary Search Tree +2 |
| 888 | Fair Candy Swap | Easy | Array, Hash Table, Binary Search +1 |
| 1064 | Fixed PointPremium | Easy | Array, Binary Search |
| 1099 | Two Sum Less Than KPremium | Easy | Array, Two Pointers, Binary Search +1 |
| 1150 | Check If a Number Is Majority Element in a Sorted ArrayPremium | Easy | Array, Binary Search |
| 1213 | Intersection of Three Sorted ArraysPremium | Easy | Array, Hash Table, Binary Search +1 |
| 1337 | The K Weakest Rows in a Matrix | Easy | Array, Binary Search, Matrix +2 |
| 1346 | Check If N and Its Double Exist | Easy | Array, Hash Table, Two Pointers +2 |
| 1351 | Count Negative Numbers in a Sorted Matrix | Easy | Array, Binary Search, Matrix |
| 1385 | Find the Distance Value Between Two Arrays | Easy | Array, Two Pointers, Binary Search +1 |
| 1539 | Kth Missing Positive Number | Easy | Array, Binary Search |
| 1608 | Special Array With X Elements Greater Than or Equal X | Easy | Array, Binary Search, Sorting |
| 2089 | Find Target Indices After Sorting Array | Easy | Array, Binary Search, Sorting |
| 2389 | Longest Subsequence With Limited Sum | Easy | Greedy, Array, Binary Search +2 |
| 2529 | Maximum Count of Positive Integer and Negative Integer | Easy | Array, Binary Search, Counting |
| 2540 | Minimum Common Value | Easy | Array, Hash Table, Two Pointers +1 |
| 2774 | Array Upper BoundPremium | Easy | JavaScript |
| 2824 | Count Pairs Whose Sum is Less than Target | Easy | Array, Two Pointers, Binary Search +1 |
| 2970 | Count the Number of Incremovable Subarrays I | Easy | Array, Two Pointers, Binary Search +1 |
Medium (138)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 33 | Search in Rotated Sorted Array | Medium | Array, Binary Search |
| 34 | Find First and Last Position of Element in Sorted Array | Medium | Array, Binary Search |
| 74 | Search a 2D Matrix | Medium | Array, Binary Search, Matrix |
| 81 | Search in Rotated Sorted Array II | Medium | Array, Binary Search |
| 153 | Find Minimum in Rotated Sorted Array | Medium | Array, Binary Search |
| 162 | Find Peak Element | Medium | Array, Binary Search |
| 167 | Two Sum II - Input Array Is Sorted | Medium | Array, Two Pointers, Binary Search |
| 209 | Minimum Size Subarray Sum | Medium | Array, Binary Search, Prefix Sum +1 |
| 240 | Search a 2D Matrix II | Medium | Array, Binary Search, Divide and Conquer +1 |
| 275 | H-Index II | Medium | Array, Binary Search |
| 287 | Find the Duplicate Number | Medium | Bit Manipulation, Array, Two Pointers +1 |
| 300 | Longest Increasing Subsequence | Medium | Array, Binary Search, Dynamic Programming |
| 378 | Kth Smallest Element in a Sorted Matrix | Medium | Array, Binary Search, Matrix +2 |
| 400 | Nth Digit | Medium | Math, Binary Search |
| 436 | Find Right Interval | Medium | Array, Binary Search, Sorting |
| 456 | 132 Pattern | Medium | Stack, Array, Binary Search +2 |
| 475 | Heaters | Medium | Array, Two Pointers, Binary Search +1 |
| 497 | Random Point in Non-overlapping Rectangles | Medium | Reservoir Sampling, Array, Math +4 |
| 528 | Random Pick with Weight | Medium | Array, Math, Binary Search +2 |
| 532 | K-diff Pairs in an Array | Medium | Array, Hash Table, Two Pointers +2 |
| 540 | Single Element in a Sorted Array | Medium | Array, Binary Search |
| 611 | Valid Triangle Number | Medium | Greedy, Array, Two Pointers +2 |
| 633 | Sum of Square Numbers | Medium | Math, Two Pointers, Binary Search |
| 658 | Find K Closest Elements | Medium | Array, Two Pointers, Binary Search +3 |
| 713 | Subarray Product Less Than K | Medium | Array, Binary Search, Prefix Sum +1 |
| 718 | Maximum Length of Repeated Subarray | Medium | Array, Binary Search, Dynamic Programming +3 |
| 729 | My Calendar I | Medium | Design, Segment Tree, Array +2 |
| 731 | My Calendar II | Medium | Design, Segment Tree, Array +3 |
| 875 | Koko Eating Bananas | Medium | Array, Binary Search |
| 981 | Time Based Key-Value Store | Medium | Design, Hash Table, String +1 |
| 1004 | Max Consecutive Ones III | Medium | Array, Binary Search, Prefix Sum +1 |
| 1268 | Search Suggestions System | Medium | Trie, Array, String +3 |
| 2300 | Successful Pairs of Spells and Potions | Medium | Array, Two Pointers, Binary Search +1 |
| 259 | 3Sum SmallerPremium | Medium | Array, Two Pointers, Binary Search +1 |
| 362 | Design Hit CounterPremium | Medium | Design, Queue, Array +2 |
| 702 | Search in a Sorted Array of Unknown SizePremium | Medium | Array, Binary Search, Interactive |
| 754 | Reach a Number | Medium | Math, Binary Search |
| 786 | K-th Smallest Prime Fraction | Medium | Array, Two Pointers, Binary Search +2 |
| 792 | Number of Matching Subsequences | Medium | Trie, Array, Hash Table +4 |
| 825 | Friends Of Appropriate Ages | Medium | Array, Two Pointers, Binary Search +1 |
| 826 | Most Profit Assigning Work | Medium | Greedy, Array, Two Pointers +2 |
| 852 | Peak Index in a Mountain Array | Medium | Array, Binary Search |
| 911 | Online Election | Medium | Design, Array, Hash Table +1 |
| 1011 | Capacity To Ship Packages Within D Days | Medium | Array, Binary Search |
| 1027 | Longest Arithmetic Subsequence | Medium | Array, Hash Table, Binary Search +1 |
| 1055 | Shortest Way to Form StringPremium | Medium | Greedy, Two Pointers, String +1 |
| 1060 | Missing Element in Sorted ArrayPremium | Medium | Array, Binary Search |
| 1062 | Longest Repeating SubstringPremium | Medium | String, Binary Search, Dynamic Programming +3 |
| 1102 | Path With Maximum Minimum ValuePremium | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 1146 | Snapshot Array | Medium | Design, Array, Hash Table +1 |
| 1170 | Compare Strings by Frequency of the Smallest Character | Medium | Array, Hash Table, String +2 |
| 1182 | Shortest Distance to Target ColorPremium | Medium | Array, Binary Search, Dynamic Programming |
| 1198 | Find Smallest Common Element in All RowsPremium | Medium | Array, Hash Table, Binary Search +2 |
| 1201 | Ugly Number III | Medium | Math, Binary Search, Combinatorics +1 |
| 1208 | Get Equal Substrings Within Budget | Medium | String, Binary Search, Prefix Sum +1 |
| 1214 | Two Sum BSTsPremium | Medium | Stack, Tree, Depth-First Search +4 |
| 1237 | Find Positive Integer Solution for a Given Equation | Medium | Math, Two Pointers, Binary Search +1 |
| 1283 | Find the Smallest Divisor Given a Threshold | Medium | Array, Binary Search |
| 1292 | Maximum Side Length of a Square with Sum Less than or Equal to Threshold | Medium | Array, Binary Search, Matrix +1 |
| 1300 | Sum of Mutated Array Closest to Target | Medium | Array, Binary Search, Sorting |
| 1348 | Tweet Counts Per Frequency | Medium | Design, Hash Table, String +3 |
| 1428 | Leftmost Column with at Least a OnePremium | Medium | Array, Binary Search, Interactive +1 |
| 1477 | Find Two Non-overlapping Sub-arrays Each With Target Sum | Medium | Array, Hash Table, Binary Search +2 |
| 1482 | Minimum Number of Days to Make m Bouquets | Medium | Array, Binary Search |
| 1488 | Avoid Flood in The City | Medium | Greedy, Array, Hash Table +2 |
| 1498 | Number of Subsequences That Satisfy the Given Sum Condition | Medium | Array, Two Pointers, Binary Search +1 |
| 1508 | Range Sum of Sorted Subarray Sums | Medium | Array, Two Pointers, Binary Search +2 |
| 1533 | Find the Index of the Large IntegerPremium | Medium | Array, Binary Search, Interactive |
| 1552 | Magnetic Force Between Two Balls | Medium | Array, Binary Search, Sorting |
| 1562 | Find Latest Group of Size M | Medium | Array, Hash Table, Binary Search +1 |
| 1574 | Shortest Subarray to be Removed to Make Array Sorted | Medium | Stack, Array, Two Pointers +2 |
| 1618 | Maximum Font to Fit a Sentence in a ScreenPremium | Medium | Array, String, Binary Search +1 |
| 1631 | Path With Minimum Effort | Medium | Depth-First Search, Breadth-First Search, Union Find +4 |
| 1648 | Sell Diminishing-Valued Colored Balls | Medium | Greedy, Array, Math +3 |
| 1658 | Minimum Operations to Reduce X to Zero | Medium | Array, Hash Table, Binary Search +2 |
| 1712 | Ways to Split Array Into Three Subarrays | Medium | Array, Two Pointers, Binary Search +1 |
| 1760 | Minimum Limit of Balls in a Bag | Medium | Array, Binary Search |
| 1802 | Maximum Value at a Given Index in a Bounded Array | Medium | Greedy, Math, Binary Search |
| 1818 | Minimum Absolute Sum Difference | Medium | Array, Binary Search, Ordered Set +1 |
| 1838 | Frequency of the Most Frequent Element | Medium | Greedy, Array, Binary Search +3 |
| 1855 | Maximum Distance Between a Pair of Values | Medium | Array, Two Pointers, Binary Search |
| 1870 | Minimum Speed to Arrive on Time | Medium | Array, Binary Search |
| 1885 | Count Pairs in Two ArraysPremium | Medium | Array, Two Pointers, Binary Search +1 |
| 1891 | Cutting RibbonsPremium | Medium | Array, Binary Search |
| 1894 | Find the Student that Will Replace the Chalk | Medium | Array, Binary Search, Prefix Sum +1 |
| 1898 | Maximum Number of Removable Characters | Medium | Array, Two Pointers, String +1 |
| 1901 | Find a Peak Element II | Medium | Array, Binary Search, Matrix |
| 1918 | Kth Smallest Subarray SumPremium | Medium | Array, Binary Search, Sliding Window |
| 1954 | Minimum Garden Perimeter to Collect Enough Apples | Medium | Math, Binary Search |
| 1966 | Binary Searchable Numbers in an Unsorted ArrayPremium | Medium | Array, Binary Search |
| 2008 | Maximum Earnings From Taxi | Medium | Array, Hash Table, Binary Search +2 |
| 2024 | Maximize the Confusion of an Exam | Medium | String, Binary Search, Prefix Sum +1 |
| 2031 | Count Subarrays With More Ones Than ZerosPremium | Medium | Binary Indexed Tree, Segment Tree, Array +5 |
| 2054 | Two Best Non-Overlapping Events | Medium | Array, Binary Search, Dynamic Programming +2 |
| 2055 | Plates Between Candles | Medium | Array, String, Binary Search +1 |
| 2064 | Minimized Maximum of Products Distributed to Any Store | Medium | Greedy, Array, Binary Search |
| 2070 | Most Beautiful Item for Each Query | Medium | Array, Binary Search, Sorting |
| 2080 | Range Frequency Queries | Medium | Design, Segment Tree, Array +2 |
| 2137 | Pour Water Between Buckets to Make Water Levels EqualPremium | Medium | Array, Binary Search |
| 2187 | Minimum Time to Complete Trips | Medium | Array, Binary Search |
| 2226 | Maximum Candies Allocated to K Children | Medium | Array, Binary Search |
| 2250 | Count Number of Rectangles Containing Each Point | Medium | Binary Indexed Tree, Array, Hash Table +2 |
| 2271 | Maximum White Tiles Covered by a Carpet | Medium | Greedy, Array, Binary Search +3 |
| 2332 | The Latest Time to Catch a Bus | Medium | Array, Two Pointers, Binary Search +1 |
| 2333 | Minimum Sum of Squared Difference | Medium | Greedy, Array, Binary Search +2 |
| 2358 | Maximum Number of Groups Entering a Competition | Medium | Greedy, Array, Math +1 |
| 2387 | Median of a Row Wise Sorted MatrixPremium | Medium | Array, Binary Search, Matrix |
| 2411 | Smallest Subarrays With Maximum Bitwise OR | Medium | Bit Manipulation, Array, Binary Search +1 |
| 2424 | Longest Uploaded Prefix | Medium | Union Find, Design, Binary Indexed Tree +5 |
| 2439 | Minimize Maximum of Array | Medium | Greedy, Array, Binary Search +2 |
| 2476 | Closest Nodes Queries in a Binary Search Tree | Medium | Tree, Depth-First Search, Binary Search Tree +3 |
| 2498 | Frog Jump II | Medium | Greedy, Array, Binary Search |
| 2501 | Longest Square Streak in an Array | Medium | Array, Hash Table, Binary Search +2 |
| 2513 | Minimize the Maximum of Two Arrays | Medium | Math, Binary Search, Number Theory |
| 2517 | Maximum Tastiness of Candy Basket | Medium | Greedy, Array, Binary Search +1 |
| 2554 | Maximum Number of Integers to Choose From a Range I | Medium | Greedy, Array, Hash Table +2 |
| 2555 | Maximize Win From Two Segments | Medium | Array, Binary Search, Sliding Window |
| 2557 | Maximum Number of Integers to Choose From a Range IIPremium | Medium | Greedy, Array, Binary Search +1 |
| 2560 | House Robber IV | Medium | Greedy, Array, Binary Search +1 |
| 2563 | Count the Number of Fair Pairs | Medium | Array, Two Pointers, Binary Search +1 |
| 2576 | Find the Maximum Number of Marked Indices | Medium | Greedy, Array, Two Pointers +2 |
| 2594 | Minimum Time to Repair Cars | Medium | Array, Binary Search |
| 2601 | Prime Subtraction Operation | Medium | Greedy, Array, Math +2 |
| 2602 | Minimum Operations to Make All Array Elements Equal | Medium | Array, Binary Search, Prefix Sum +1 |
| 2616 | Minimize the Maximum Difference of Pairs | Medium | Greedy, Array, Binary Search +2 |
| 2779 | Maximum Beauty of an Array After Applying Operation | Medium | Array, Binary Search, Sorting +1 |
| 2812 | Find the Safest Path in a Grid | Medium | Breadth-First Search, Union Find, Array +3 |
| 2817 | Minimum Absolute Difference Between Elements With Constraint | Medium | Array, Binary Search, Ordered Set |
| 2826 | Sorting Three Groups | Medium | Array, Binary Search, Dynamic Programming |
| 2830 | Maximize the Profit as the Salesman | Medium | Array, Hash Table, Binary Search +2 |
| 2831 | Find the Longest Equal Subarray | Medium | Array, Hash Table, Binary Search +1 |
| 2838 | Maximum Coins Heroes Can CollectPremium | Medium | Array, Two Pointers, Binary Search +2 |
| 2856 | Minimum Array Length After Pair Removals | Medium | Greedy, Array, Hash Table +3 |
| 2861 | Maximum Number of Alloys | Medium | Array, Binary Search |
| 2936 | Number of Equal Numbers BlocksPremium | Medium | Array, Binary Search, Interactive |
| 2967 | Minimum Cost to Make Array Equalindromic | Medium | Greedy, Array, Math +2 |
| 2981 | Find Longest Special Substring That Occurs Thrice I | Medium | Hash Table, String, Binary Search +2 |
| 2982 | Find Longest Special Substring That Occurs Thrice II | Medium | Hash Table, String, Binary Search +2 |
Hard (85)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 4 | Median of Two Sorted Arrays | Hard | Array, Binary Search, Divide and Conquer |
| 154 | Find Minimum in Rotated Sorted Array II | Hard | Array, Binary Search |
| 315 | Count of Smaller Numbers After Self | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 327 | Count of Range Sum | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 352 | Data Stream as Disjoint Intervals | Hard | Union Find, Design, Hash Table +3 |
| 354 | Russian Doll Envelopes | Hard | Array, Binary Search, Dynamic Programming +1 |
| 363 | Max Sum of Rectangle No Larger Than K | Hard | Array, Binary Search, Matrix +2 |
| 410 | Split Array Largest Sum | Hard | Greedy, Array, Binary Search +2 |
| 483 | Smallest Good Base | Hard | Math, Binary Search |
| 493 | Reverse Pairs | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 668 | Kth Smallest Number in Multiplication Table | Hard | Math, Binary Search |
| 710 | Random Pick with Blacklist | Hard | Array, Hash Table, Math +3 |
| 719 | Find K-th Smallest Pair Distance | Hard | Array, Two Pointers, Binary Search +1 |
| 732 | My Calendar III | Hard | Design, Segment Tree, Binary Search +2 |
| 778 | Swim in Rising Water | Hard | Depth-First Search, Breadth-First Search, Union Find +4 |
| 1235 | Maximum Profit in Job Scheduling | Hard | Array, Binary Search, Dynamic Programming +1 |
| 1851 | Minimum Interval to Include Each Query | Hard | Array, Binary Search, Sorting +2 |
| 302 | Smallest Rectangle Enclosing Black PixelsPremium | Hard | Depth-First Search, Breadth-First Search, Array +2 |
| 644 | Maximum Average Subarray IIPremium | Hard | Array, Binary Search, Prefix Sum |
| 774 | Minimize Max Distance to Gas StationPremium | Hard | Array, Binary Search |
| 793 | Preimage Size of Factorial Zeroes Function | Hard | Math, Binary Search |
| 862 | Shortest Subarray with Sum at Least K | Hard | Queue, Array, Binary Search +4 |
| 878 | Nth Magical Number | Hard | Math, Binary Search |
| 887 | Super Egg Drop | Hard | Math, Binary Search, Dynamic Programming |
| 902 | Numbers At Most N Given Digit Set | Hard | Array, Math, String +2 |
| 1044 | Longest Duplicate Substring | Hard | String, Binary Search, Suffix Array +3 |
| 1095 | Find in Mountain Array | Hard | Array, Binary Search, Interactive |
| 1157 | Online Majority Element In Subarray | Hard | Design, Binary Indexed Tree, Segment Tree +2 |
| 1187 | Make Array Strictly Increasing | Hard | Array, Binary Search, Dynamic Programming +1 |
| 1231 | Divide ChocolatePremium | Hard | Array, Binary Search |
| 1439 | Find the Kth Smallest Sum of a Matrix With Sorted Rows | Hard | Array, Binary Search, Matrix +1 |
| 1483 | Kth Ancestor of a Tree Node | Hard | Bit Manipulation, Tree, Depth-First Search +4 |
| 1521 | Find a Value of a Mysterious Function Closest to Target | Hard | Bit Manipulation, Segment Tree, Array +1 |
| 1649 | Create Sorted Array through Instructions | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 1671 | Minimum Number of Removals to Make Mountain Array | Hard | Greedy, Array, Binary Search +1 |
| 1713 | Minimum Operations to Make a Subsequence | Hard | Greedy, Array, Hash Table +1 |
| 1739 | Building Boxes | Hard | Greedy, Math, Binary Search |
| 1751 | Maximum Number of Events That Can Be Attended II | Hard | Array, Binary Search, Dynamic Programming +1 |
| 1782 | Count Pairs Of Nodes | Hard | Graph, Array, Hash Table +4 |
| 1793 | Maximum Score of a Good Subarray | Hard | Stack, Array, Two Pointers +2 |
| 1847 | Closest Room | Hard | Array, Binary Search, Ordered Set +1 |
| 1862 | Sum of Floored Pairs | Hard | Array, Math, Binary Search +1 |
| 1889 | Minimum Space Wasted From Packaging | Hard | Array, Binary Search, Prefix Sum +1 |
| 1923 | Longest Common Subpath | Hard | Array, Binary Search, Suffix Array +2 |
| 1932 | Merge BSTs to Create Single BST | Hard | Tree, Depth-First Search, Hash Table +2 |
| 1956 | Minimum Time For K Virus Variants to SpreadPremium | Hard | Geometry, Array, Math +2 |
| 1964 | Find the Longest Valid Obstacle Course at Each Position | Hard | Binary Indexed Tree, Array, Binary Search |
| 1970 | Last Day Where You Can Still Cross | Hard | Depth-First Search, Breadth-First Search, Union Find +3 |
| 2009 | Minimum Number of Operations to Make Array Continuous | Hard | Array, Hash Table, Binary Search +1 |
| 2035 | Partition Array Into Two Arrays to Minimize Sum Difference | Hard | Bit Manipulation, Array, Two Pointers +4 |
| 2040 | Kth Smallest Product of Two Sorted Arrays | Hard | Array, Binary Search |
| 2071 | Maximum Number of Tasks You Can Assign | Hard | Greedy, Queue, Array +4 |
| 2106 | Maximum Fruits Harvested After at Most K Steps | Hard | Array, Binary Search, Prefix Sum +1 |
| 2111 | Minimum Operations to Make the Array K-Increasing | Hard | Array, Binary Search |
| 2141 | Maximum Running Time of N Computers | Hard | Greedy, Array, Binary Search +1 |
| 2179 | Count Good Triplets in an Array | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 2223 | Sum of Scores of Built Strings | Hard | String, Binary Search, String Matching +3 |
| 2234 | Maximum Total Beauty of the Gardens | Hard | Greedy, Array, Two Pointers +4 |
| 2251 | Number of Flowers in Full Bloom | Hard | Array, Hash Table, Binary Search +3 |
| 2258 | Escape the Spreading Fire | Hard | Breadth-First Search, Array, Binary Search +1 |
| 2286 | Booking Concert Tickets in Groups | Hard | Design, Binary Indexed Tree, Segment Tree +1 |
| 2302 | Count Subarrays With Score Less Than K | Hard | Array, Binary Search, Prefix Sum +1 |
| 2354 | Number of Excellent Pairs | Hard | Bit Manipulation, Array, Hash Table +1 |
| 2398 | Maximum Number of Robots Within Budget | Hard | Queue, Array, Binary Search +4 |
| 2426 | Number of Pairs Satisfying Inequality | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 2448 | Minimum Cost to Make Array Equal | Hard | Greedy, Array, Binary Search +2 |
| 2454 | Next Greater Element IV | Hard | Stack, Array, Binary Search +3 |
| 2468 | Split Message Based on Limit | Hard | String, Binary Search, Enumeration |
| 2519 | Count the Number of K-Big IndicesPremium | Hard | Binary Indexed Tree, Segment Tree, Array +4 |
| 2528 | Maximize the Minimum Powered City | Hard | Greedy, Queue, Array +3 |
| 2565 | Subsequence With the Minimum Score | Hard | Two Pointers, String, Binary Search |
| 2589 | Minimum Time to Complete All Tasks | Hard | Stack, Greedy, Array +2 |
| 2604 | Minimum Time to Eat All GrainsPremium | Hard | Array, Two Pointers, Binary Search +1 |
| 2659 | Make Array Empty | Hard | Greedy, Binary Indexed Tree, Segment Tree +4 |
| 2702 | Minimum Operations to Make Numbers Non-positivePremium | Hard | Array, Binary Search |
| 2713 | Maximum Strictly Increasing Cells in a Matrix | Hard | Memoization, Array, Hash Table +5 |
| 2736 | Maximum Sum Queries | Hard | Stack, Binary Indexed Tree, Segment Tree +4 |
| 2790 | Maximum Number of Groups With Increasing Length | Hard | Greedy, Array, Math +2 |
| 2819 | Minimum Relative Loss After Buying ChocolatesPremium | Hard | Array, Binary Search, Prefix Sum +1 |
| 2926 | Maximum Balanced Subsequence Sum | Hard | Binary Indexed Tree, Segment Tree, Array +2 |
| 2940 | Find Building Where Alice and Bob Can Meet | Hard | Stack, Binary Indexed Tree, Segment Tree +4 |
| 2941 | Maximum GCD-Sum of a SubarrayPremium | Hard | Array, Math, Binary Search +1 |
| 2945 | Find Maximum Non-decreasing Array Length | Hard | Stack, Queue, Array +4 |
| 2968 | Apply Operations to Maximize Frequency Score | Hard | Array, Binary Search, Prefix Sum +2 |
| 2972 | Count the Number of Incremovable Subarrays II | Hard | Array, Two Pointers, Binary Search |
Related patterns
Problems sit in more than one pattern more often than not, and the overlap is where the interesting follow-up questions live.
Binary Search pattern FAQ
What is the binary search pattern?
Binary search needs a sorted array far less than it needs a monotone predicate — some property that is false for every candidate below a threshold and true for every candidate above it.
How many LeetCode problems use the binary search pattern?
This page lists 254 LeetCode problems that the binary search pattern applies to: 31 Easy, 138 Medium and 85 Hard. 213 of them carry a complete Python solution with complexity analysis.
What is the time complexity of the binary search pattern?
O(log n) time and O(1) space. Every iteration discards half of what is left, so the loop runs log₂ n times. When the search is over a range of possible answers rather than over an array, the cost is O(log W) iterations times whatever the feasibility check costs, where W is the width of the range — that product is the figure to quote, because the check is usually a linear scan and the real complexity is O(n log W). The iterative form uses constant space.
When should I use the binary search pattern in an interview?
The array is sorted, or rotated-sorted, and you want a position rather than a scan. The answer is a number in a known range and checking a candidate is cheaper than finding it.
Which binary search problem should I start with?
LeetCode 35. Search Insert Position is the lowest-numbered Easy problem on this page, which makes it the usual starting point: the technique is visible without the problem's own complications getting in the way.
What patterns are related to binary search?
Two Pointers, Sorting, Greedy, Heap / Priority Queue. Problems frequently sit in more than one of these, and the overlap is where the interesting follow-up questions come from.
More ways in: all 22 patterns, the curated study lists, or the full problem list.
Meet the binary search problem you did not practise
Stealth Interview is a desktop app for macOS and Windows. It reads the coding problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer is saying — while staying invisible to screen sharing.