Guess Number Higher or Lower — Interactive & Iterators

Find a hidden value within a known range by asking a limited number of questions, each reply saying higher, lower or correct.

Interactive & Iterators · Easy

The problem

This is a game of twenty questions played with numbers: a friend has thought of a secret and answers each of your guesses with a direction. A secret number between 1 and n has been picked. A provided function guess(num) compares your number to the secret and tells you which way to go; calls are limited. Find the secret number.

The story

A friend picks a secret number from 1 to n, with n at most 100. Each guess you make gets an answer: -1 if your guess is too high, 1 if it's too low, and 0 if it's right. You get at most 9 guesses. Find the secret.

What to return

Constraints

Example

Input
n = 12
Output
9

Solve it

More in Interactive & Iterators

All algorithm problems