easyBinary SearchBinary Search

Guess Number Higher or Lower

easyTime: O(log n)Space: O(1)

Recognize the pattern

sorted range 1 to nguess higher or lowerminimize guesses

Brute force idea

A straightforward first read of Guess Number Higher or Lower is this: Try every number from 1 to n. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

Better approach

The real unlock in Guess Number Higher or Lower comes when you notice this: Binary search the range: guess the midpoint, adjust range based on feedback. Instead of recomputing the world every time, you preserve just enough context to let the next decision become obvious.

Key invariant

The compass for Guess Number Higher or Lower is this: Each guess eliminates half the remaining candidates. As long as that statement keeps holding, you can trust the steps built on top of it.

Watch out for

One easy way to drift off course in Guess Number Higher or Lower is this: Integer overflow when calculating mid — use left + (right - left) / 2 instead of (left + right) / 2. The fix is usually to return to the meaning of each move, not just the steps themselves.

Binary Search Pattern