Use binary search with API feedback.
1public class Solution extends GuessGame {
2 public int guessNumber(int n) {
3 int left = 1, right = n;
4
5 while (left <= right) {
6 int mid = left + (right - left) / 2;
7 int result = guess(mid);
8
9 if (result == 0) {
10 return mid;
11 } else if (result < 0) {
12 right = mid - 1;
13 } else {
14 left = mid + 1;
15 }
16 }
17
18 return -1;
19 }
20}