#include <leetcode/utils.h>
#include <stdio.h>

static int pick = 0;

/**
 * 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);
 */
int guess(int num)
{
    if (num > pick)
        return -1;
    if (num < pick)
        return 1;
    return 0;
}

int guessNumber(int n)
{
    int low = 1, high = n;
    while (low < high)
    {
        int res = guess((low + high) / 2);
        if (res == -1)
            high = (n + 1) / 2;
        else if (res == 1)
            low = (low + high) / 2;
        else
            return res;
    }

    return high + guess(high);
}

int main()
{
    pick = 6;
    ASSERT(guessNumber(10), pick, fail);

    pick = 1;
    ASSERT(guessNumber(2), pick, fail);

    return 0;

fail:
    return -1;
}
