DEV Community

Ganga Sri V
Ganga Sri V

Posted on

Guess Number

Problem Statement:
I pick a number from 1 to n. You have to guess which number I picked (the number I picked stays the same throughout the game).Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.Return the number that I picked.

My Approach:

1.First I start with low = 1 and high = n to find the range of possible numbers.
2.Then I guess the middle number in the current range using the guess() API given in the problem.
3.If my guess is correct (0), I return it immediately.
4.If my guess is too high (-1), I move high just before my guess to search the lower one of the array.
5.If my guess is too low (1), I move low just after my guess to search the upper one of the array.
6.Then I repeat this process until I find the picked number

Top comments (0)