We are playing the Guess Game. I pick a number from 1 to n, and you have to guess which number I picked.
Every time you guess wrong, I tell you whether the number I picked is higher or lower than your guess.
You find out through a pre-defined API, guess(num), which returns one of three results:
-1: your guess is too high (num > pick).1: your guess is too low (num < pick).0: your guess is right (num == pick).
Return the number that I picked.
Examples
Example 1
Input: n = 10, pick = 6
Output: 6
One way to get there: guess(5) returns 1 (too low), so the pick is in 6–10. guess(8) returns -1 (too high), so it's in 6–7. guess(6) returns 0.
Example 2
Input: n = 1, pick = 1
Output: 1
There's only one possible number.
Example 3
Input: n = 2, pick = 1
Output: 1
Example 4
Input: n = 2147483647, pick = 2147483647
Output: 2147483647
n is the largest 32-bit integer. With both ends of the range this high, (low + high) / 2 overflows an int in Java, C++ and C.
Constraints
1 <= n <= 2^31 - 11 <= pick <= n
How the tests work: each test lists
nandpick, but your function only receivesn. The only way to learnpickis to callguess. In TypeScript, Python, C and C++,guessis a global function; in Java,SolutionextendsGuessGame, which provides it. More than 100 calls toguessfails the test.