Find a hidden number by repeatedly guessing and using higher/lower feedback.
You are playing a guessing game with a hidden integer chosen from the range . Each time you make a guess, you receive feedback:
-1 if your guess is higher than the hidden number1 if your guess is lower than the hidden number0 if your guess is exactly correctYour task is to determine the hidden number using as few guesses as possible.
Assume you have access to an API guess(num) that returns the feedback above. Implement a function that finds the hidden number.
n representing the inclusive upper bound of the search range.guess(num) that compares num with the hidden number and returns -1, 1, or 0.Return the hidden number.
guess(num).Example 1
Input
n = 10, hidden number = 6
Output
6
Explanation
If you guess 5 and get 1, the hidden number is larger. If you then guess 8 and get -1, it is smaller. Continue narrowing the range until you find 6.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.