Guess Number Higher or Lower

PHOTO EMBED

Thu Apr 06 2023 03:27:28 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

/** 
 * 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);
 */

class Solution {
public:
    // int guess(int num)
    // {
    //     if(num>)
    // }
    int guessNumber(int n) {
        int s=1, e=n;
        int mid=s+(e-s)/2;
        
        while(s<=e)
        {
            int num=guess(mid);
            if(num==0) return mid;
            if(num==-1) e=mid-1;
            else s=mid+1;
            mid=s+(e-s)/2;
        }
        return mid;
        
    }
};
content_copyCOPY

https://leetcode.com/problems/guess-number-higher-or-lower/