← Lessons

quiz vs the machine

Gold1460

Algorithms

Modified Binary Search Pattern

Adapt the halving search to rotated arrays, boundaries, and answer spaces.

6 min read · core · beat Gold to climb

More than finding an exact value

Plain binary search looks for a target in a sorted array by halving the range each step. The modified binary search pattern keeps that halving discipline but bends it to answer richer questions: the first or last position of a value, a rotation point, a ceiling, or even a numeric answer that is not stored in any array.

Choosing which half to keep

Every variant rests on the same engine. You maintain a low and high boundary and inspect the middle.

  • Decide, using a problem specific test, whether the answer lies left or right of the middle.
  • Discard the half that cannot contain the answer by moving low or high.
  • Keep narrowing until the boundaries meet.

The art is the test. For a rotated array you compare the middle against the ends to learn which side is still sorted, then check whether the target falls in that sorted half.

Searching an answer space

Some problems have no array at all. To find the smallest capacity or speed that satisfies a constraint, you binary search over the range of possible answers, using a feasibility check as the test. This turns an optimization into a sequence of yes or no questions.

Key idea

Keep binary search halving but replace the equality check with a problem specific test, so it locates boundaries, rotation points, and feasible answers, not just exact matches.

Check yourself

Answer to earn rating on the learn ladder.

1. What stays constant across all modified binary search variants?

2. How do you search for a minimum feasible value when no array exists?

3. In a rotated sorted array, how do you decide which side to search?