Binary Search LeetCode Problems: All 253,
With Python Solutions

Every problem in this library that LeetCode tags Binary Search. 213 of them carry a complete Python solution, a worked example and the time and space complexity of the approach.

Easy
30
Medium
138
Hard
85
1 pattern

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.

Eyes only

When the Binary Search problem arrives live.

In the live round, Stealth Interview reads the 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 says. It's a desktop app for macOS and Windows, invisible to screen sharing.

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)

Easy problems
No.Problem
0035Search Insert Position
0069Sqrt(x)
0222Count Complete Tree Nodes
0268Missing Number
0278First Bad Version
0349Intersection of Two Arrays
0350Intersection of Two Arrays II
0367Valid Perfect Square
0374Guess Number Higher or Lower
0441Arranging Coins
0704Binary Search
0744Find Smallest Letter Greater Than Target
0888Fair Candy Swap
1337The K Weakest Rows in a Matrix
1346Check If N and Its Double Exist
1351Count Negative Numbers in a Sorted Matrix
1385Find the Distance Value Between Two Arrays
1539Kth Missing Positive Number
1608Special Array With X Elements Greater Than or Equal X
2089Find Target Indices After Sorting Array
2389Longest Subsequence With Limited Sum
2529Maximum Count of Positive Integer and Negative Integer
2540Minimum Common Value
2824Count Pairs Whose Sum is Less than Target

Medium (108)

Medium problems
No.Problem
0033Search in Rotated Sorted Array
0034Find First and Last Position of Element in Sorted Array
0074Search a 2D Matrix
0081Search in Rotated Sorted Array II
0153Find Minimum in Rotated Sorted Array
0162Find Peak Element
0167Two Sum II - Input Array Is Sorted
0209Minimum Size Subarray Sum
0240Search a 2D Matrix II
0275H-Index II
0287Find the Duplicate Number
0300Longest Increasing Subsequence
0378Kth Smallest Element in a Sorted Matrix
0400Nth Digit
0436Find Right Interval
0456132 Pattern
0475Heaters
0497Random Point in Non-overlapping Rectangles
0528Random Pick with Weight
0532K-diff Pairs in an Array
0540Single Element in a Sorted Array
0611Valid Triangle Number
0633Sum of Square Numbers
0658Find K Closest Elements
0713Subarray Product Less Than K
0718Maximum Length of Repeated Subarray
0729My Calendar I
0731My Calendar II
0754Reach a Number
0786K-th Smallest Prime Fraction
0792Number of Matching Subsequences
0825Friends Of Appropriate Ages
0826Most Profit Assigning Work
0852Peak Index in a Mountain Array
0875Koko Eating Bananas
0911Online Election
0981Time Based Key-Value Store
1004Max Consecutive Ones III
1011Capacity To Ship Packages Within D Days
1027Longest Arithmetic Subsequence
1146Snapshot Array
1170Compare Strings by Frequency of the Smallest Character
1201Ugly Number III
1208Get Equal Substrings Within Budget
1237Find Positive Integer Solution for a Given Equation
1268Search Suggestions System
1283Find the Smallest Divisor Given a Threshold
1292Maximum Side Length of a Square with Sum Less than or Equal to Threshold
1300Sum of Mutated Array Closest to Target
1348Tweet Counts Per Frequency
1477Find Two Non-overlapping Sub-arrays Each With Target Sum
1482Minimum Number of Days to Make m Bouquets
1488Avoid Flood in The City
1498Number of Subsequences That Satisfy the Given Sum Condition
1508Range Sum of Sorted Subarray Sums
1552Magnetic Force Between Two Balls
1562Find Latest Group of Size M
1574Shortest Subarray to be Removed to Make Array Sorted
1631Path With Minimum Effort
1648Sell Diminishing-Valued Colored Balls
1658Minimum Operations to Reduce X to Zero
1712Ways to Split Array Into Three Subarrays
1760Minimum Limit of Balls in a Bag
1802Maximum Value at a Given Index in a Bounded Array
1818Minimum Absolute Sum Difference
1838Frequency of the Most Frequent Element
1855Maximum Distance Between a Pair of Values
1870Minimum Speed to Arrive on Time
1894Find the Student that Will Replace the Chalk
1898Maximum Number of Removable Characters
1901Find a Peak Element II
1954Minimum Garden Perimeter to Collect Enough Apples
2008Maximum Earnings From Taxi
2024Maximize the Confusion of an Exam
2054Two Best Non-Overlapping Events
2055Plates Between Candles
2064Minimized Maximum of Products Distributed to Any Store
2070Most Beautiful Item for Each Query
2080Range Frequency Queries
2187Minimum Time to Complete Trips
2226Maximum Candies Allocated to K Children
2250Count Number of Rectangles Containing Each Point
2271Maximum White Tiles Covered by a Carpet
2300Successful Pairs of Spells and Potions
2332The Latest Time to Catch a Bus
2333Minimum Sum of Squared Difference
2358Maximum Number of Groups Entering a Competition
2411Smallest Subarrays With Maximum Bitwise OR
2424Longest Uploaded Prefix
2439Minimize Maximum of Array
2476Closest Nodes Queries in a Binary Search Tree
2498Frog Jump II
2501Longest Square Streak in an Array
2513Minimize the Maximum of Two Arrays
2517Maximum Tastiness of Candy Basket
2554Maximum Number of Integers to Choose From a Range I
2555Maximize Win From Two Segments
2560House Robber IV
2563Count the Number of Fair Pairs
2576Find the Maximum Number of Marked Indices
2594Minimum Time to Repair Cars
2601Prime Subtraction Operation
2602Minimum Operations to Make All Array Elements Equal
2616Minimize the Maximum Difference of Pairs
2779Maximum Beauty of an Array After Applying Operation
2812Find the Safest Path in a Grid
2817Minimum Absolute Difference Between Elements With Constraint
2826Sorting Three Groups

Hard (68)

Hard problems
No.Problem
0004Median of Two Sorted Arrays
0154Find Minimum in Rotated Sorted Array II
0315Count of Smaller Numbers After Self
0327Count of Range Sum
0352Data Stream as Disjoint Intervals
0354Russian Doll Envelopes
0363Max Sum of Rectangle No Larger Than K
0410Split Array Largest Sum
0483Smallest Good Base
0493Reverse Pairs
0668Kth Smallest Number in Multiplication Table
0710Random Pick with Blacklist
0719Find K-th Smallest Pair Distance
0732My Calendar III
0778Swim in Rising Water
0793Preimage Size of Factorial Zeroes Function
0862Shortest Subarray with Sum at Least K
0878Nth Magical Number
0887Super Egg Drop
0902Numbers At Most N Given Digit Set
1044Longest Duplicate Substring
1095Find in Mountain Array
1157Online Majority Element In Subarray
1187Make Array Strictly Increasing
1235Maximum Profit in Job Scheduling
1439Find the Kth Smallest Sum of a Matrix With Sorted Rows
1483Kth Ancestor of a Tree Node
1521Find a Value of a Mysterious Function Closest to Target
1649Create Sorted Array through Instructions
1671Minimum Number of Removals to Make Mountain Array
1713Minimum Operations to Make a Subsequence
1739Building Boxes
1751Maximum Number of Events That Can Be Attended II
1782Count Pairs Of Nodes
1793Maximum Score of a Good Subarray
1847Closest Room
1851Minimum Interval to Include Each Query
1862Sum of Floored Pairs
1889Minimum Space Wasted From Packaging
1923Longest Common Subpath
1964Find the Longest Valid Obstacle Course at Each Position
1970Last Day Where You Can Still Cross
2009Minimum Number of Operations to Make Array Continuous
2035Partition Array Into Two Arrays to Minimize Sum Difference
2040Kth Smallest Product of Two Sorted Arrays
2071Maximum Number of Tasks You Can Assign
2106Maximum Fruits Harvested After at Most K Steps
2111Minimum Operations to Make the Array K-Increasing
2141Maximum Running Time of N Computers
2179Count Good Triplets in an Array
2234Maximum Total Beauty of the Gardens
2251Number of Flowers in Full Bloom
2258Escape the Spreading Fire
2286Booking Concert Tickets in Groups
2302Count Subarrays With Score Less Than K
2354Number of Excellent Pairs
2398Maximum Number of Robots Within Budget
2426Number of Pairs Satisfying Inequality
2448Minimum Cost to Make Array Equal
2454Next Greater Element IV
2468Split Message Based on Limit
2528Maximize the Minimum Powered City
2565Subsequence With the Minimum Score
2589Minimum Time to Complete All Tasks
2659Make Array Empty
2713Maximum Strictly Increasing Cells in a Matrix
2736Maximum Sum Queries
2790Maximum Number of Groups With Increasing Length