AlgoViz
HomeBinary Search

Binary Search

Halve the search space — on 1D arrays, on the answer, and in 2D grids.

0/ 51 understood · 0%
Walkthroughs
51
Problems
51
Start here
51 shown
  1. BS on 1D Arrays
  2. Search X in Sorted ArrayDiscard half the array each comparisonEasy
  3. Lower BoundFirst index with a[i] ≥ xEasy
  4. Upper BoundFirst index with a[i] > xEasy
  5. Search Insert PositionLower bound: first index ≥ targetEasy
  6. Floor and Ceil in Sorted ArrayLargest ≤ x and smallest ≥ xEasy
  7. First and Last OccurrencelowerBound(x) and upperBound(x) − 1Easy
  8. Count Occurrences in Sorted ArrayupperBound − lowerBoundEasy
  9. Search in Rotated Sorted Array IOne half is always sorted — use itMedium
  10. Search in Rotated Sorted Array IIDuplicates blur the sorted halfMedium
  11. Minimum in Rotated Sorted ArrayTake the min of the sorted halfMedium
  12. Rotation CountIndex of the minimum = rotationsEasy
  13. Single Element in Sorted ArrayParity of indices points the wayMedium
  14. Find Peak ElementClimb toward the higher neighbourMedium
  15. BS on Answers
  16. Square Root of a NumberLargest m with m·m ≤ xEasy
  17. Nth Root of a NumberLargest m with mⁿ ≤ xMedium
  18. Koko Eating BananasBinary search on the answer (eating speed)Medium
  19. Minimum Days to Make M BouquetsSearch the bloom dayMedium
  20. Smallest Divisor Given a ThresholdBigger divisor → smaller sumMedium
  21. Capacity to Ship Packages in D DaysBigger capacity → fewer daysMedium
  22. Kth Missing Positive NumberMissing before a[i] = a[i] − (i+1)Easy
  23. Aggressive CowsMaximize the minimum spacingHard
  24. Book AllocationMinimize the maximum pagesHard
  25. Split Array — Largest SumSame as book allocationHard
  26. Painter's PartitionMinimize the slowest painterMedium
  27. Minimize Max Distance to Gas StationBinary search on a real numberHard
  28. Median of Two Sorted ArraysBinary search the partitionHard
  29. Kth Element of Two Sorted ArraysPartition so the left holds kMedium
  30. BS on 2D Arrays
  31. Row with Maximum 1'sCount 1s per row with lower boundEasy
  32. Search in a 2D MatrixFlatten to one sorted arrayMedium
  33. Search in a 2D Matrix IIStaircase from the top-rightMedium
  34. Find Peak Element IIBinary search across columnsMedium
  35. Matrix MedianBinary search the value, count ≤ midHard
  36. Practice problems
  37. Count Occurrences in a Sorted ArrayLast occurrence minus first, plus oneEasy
  38. Find minimum in Rotated Sorted ArrayCompare mid with the right endEasy
  39. Find out how many times the array is rotatedThe index of the minimum is the rotation countEasy
  40. Single element in a Sorted ArrayPair parity tells you which side to keepMedium
  41. Find square root of a numberBinary search the answer, not the arrayMedium
  42. Find Nth root of a numberSame boundary search, with a powerMedium
  43. Find the smallest divisorBinary search the divisorMedium
  44. Capacity to Ship Packages Within D DaysBinary search the ship's capacityMedium
  45. Book Allocation ProblemMinimise the largest allocationHard
  46. Median of 2 sorted arraysBinary search the split pointHard
  47. Kth element of 2 sorted arraysThe median search, generalisedMedium
  48. Find row with maximum 1'sBinary search each row for its first 1Easy
  49. Search in 2D matrix - IIStart at a corner where one move goes each wayHard
  50. Binary SearchHalve the range every stepMedium
  51. Apple Harvest (Koko Eating Bananas)Binary search the eating speedMedium
  52. Search in Rotated Sorted ArrayOne half is always sorted — check whichMedium
  53. Search a 2Treat the matrix as one flat sorted arrayMedium
  54. Kth Smallest in a Sorted MatrixBinary search the value, count what is belowMedium
  55. Minimum Shipping CapacityThe same capacity searchMedium