Binary Search LeetCode Problems: All 253, With Python Solutions
Every problem in this library that LeetCode tags Binary Search — 253 in total, 213 of them with a complete Python solution, a worked example and the time and space complexity of the approach.
- 253 problems
- 30 Easy
- 138 Medium
- 85 Hard
How Binary Search problems are solved
A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Binary Search problems — each one explains the approach, gives a Python template and states its complexity.
- Binary Search — Halve the search space each step — over an array, or over the answer itself.
Binary Search problems by difficulty
Showing the first 200 of 253 problems. Problems with a complete Python solution are listed first, then by ascending problem number.
Easy (24)
| # | 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 |
| 888 | Fair Candy Swap | 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 |
| 2824 | Count Pairs Whose Sum is Less than Target | Easy | Array, Two Pointers, Binary Search +1 |
Medium (108)
| # | 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 |
| 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 |
| 875 | Koko Eating Bananas | Medium | Array, Binary Search |
| 911 | Online Election | Medium | Design, Array, Hash Table +1 |
| 981 | Time Based Key-Value Store | Medium | Design, Hash Table, String +1 |
| 1004 | Max Consecutive Ones III | Medium | Array, Binary Search, Prefix Sum +1 |
| 1011 | Capacity To Ship Packages Within D Days | Medium | Array, Binary Search |
| 1027 | Longest Arithmetic Subsequence | Medium | Array, Hash Table, Binary Search +1 |
| 1146 | Snapshot Array | Medium | Design, Array, Hash Table +1 |
| 1170 | Compare Strings by Frequency of the Smallest Character | Medium | Array, Hash Table, String +2 |
| 1201 | Ugly Number III | Medium | Math, Binary Search, Combinatorics +1 |
| 1208 | Get Equal Substrings Within Budget | Medium | String, Binary Search, Prefix Sum +1 |
| 1237 | Find Positive Integer Solution for a Given Equation | Medium | Math, Two Pointers, Binary Search +1 |
| 1268 | Search Suggestions System | Medium | Trie, Array, String +3 |
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |
| 1954 | Minimum Garden Perimeter to Collect Enough Apples | Medium | Math, 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 |
| 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 |
| 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 |
| 2300 | Successful Pairs of Spells and Potions | Medium | Array, Two Pointers, Binary Search +1 |
| 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 |
| 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 |
| 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 |
Hard (68)
| # | 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 |
| 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 |
| 1235 | Maximum Profit in Job Scheduling | Hard | Array, Binary Search, Dynamic Programming +1 |
| 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 |
| 1851 | Minimum Interval to Include Each Query | Hard | Array, Binary Search, Sorting +2 |
| 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 |
| 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 |
| 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 |
| 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 |
| 2659 | Make Array Empty | Hard | Greedy, Binary Indexed Tree, Segment Tree +4 |
| 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 |
Keep exploring
- Array1,569
- String672
- Hash Table588
- Math485
- Dynamic Programming481
- Sorting392
- Greedy346
- Depth-First Search289
- Database249
- Tree225
- Breadth-First Search223
- Matrix216
- Two Pointers201
- Bit Manipulation194
- Binary Tree174
- Heap (Priority Queue)163
- Prefix Sum157
- Stack157
- Simulation144
- Graph138
- Counting126
- Design122
- Sliding Window116
- Backtracking105
When the Binary Search problem arrives live
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.