We are playing the Guess Game. The game is as follows:

I pick a number from 1 to n. You have to guess which number I picked.

Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.

You call a pre-defined API int guess(int num), which returns three possible results:


	-1: Your guess is higher than the number I picked (i.e. num &gt; pick).
	1: Your guess is lower than the number I picked (i.e. num &lt; pick).
	0: your guess is equal to the number I picked (i.e. num == pick).


Return the number that I picked.

&nbsp;
Example 1:

Input: n = 10, pick = 6
Output: 6


Example 2:

Input: n = 1, pick = 1
Output: 1


Example 3:

Input: n = 2, pick = 1
Output: 1


&nbsp;
Constraints:


	1 &lt;= n &lt;= 231 - 1
	1 &lt;= pick &lt;= n


class Solution:
    def guessNumber(self, n: int) -> int:
        l=1
        h=n
        while l<=h:
            mid=(l+h)//2
            x =guess(mid)
            if(x==0):
                return mid
            elif(x==1):
                l = mid+1
            else:
                h = mid-1
/** 
 * Forward declaration of guess API.
 * @param  num   your guess
 * @return 	     -1 if num is higher than the picked number
 *			      1 if num is lower than the picked number
 *               otherwise return 0
 * int guess(int num);
 */

public class Solution extends GuessGame {
    public int guessNumber(int n) {
        int left = 1;
        int right = n;
        
        while(left < right){
            int mid = ((right - left) / 2) + left;
            if(guess(mid) == 0)
                return mid;
            else if(guess(mid) < 0)
                right = mid - 1;
            else
                left = mid + 1;
        }
        
        return left;
    }
}
class Solution {
public:
    int guessNumber(int n) {
        int s = 1, e = n;
        int mid = s + (e - s)/2;
        
        while (s <= e){
            if (guess(mid) == 0){   
                return mid;
            }
            
            else if (guess(mid) == -1){
                e = mid - 1;
            }
            
            else if (guess(mid) == 1){
                s = mid +1;
            }
            
            mid = s + (e - s)/2;
        }
        
        return mid;
    }
};
/** 
 * Forward declaration of guess API.
 * @param {number} num   your guess
 * @return 	     -1 if num is higher than the picked number
 *			      1 if num is lower than the picked number
 *               otherwise return 0
 * var guess = function(num) {}
 */

/**
 * @param {number} n
 * @return {number}
 */
var guessNumber = function(n) {
    let lower=1;
    let higher=n;
    while(lower<=higher){
        let mid=Math.floor((lower+higher)/2);
        if(guess(mid)==0){
            return mid;            
        }
        else if(guess(mid)==-1){
            higher=mid-1;
        }
        else{
            lower=mid+1;
        }
    }
    return 0;
    
};
Guess Number Higher or Lower