package com.fishercoder.solutions;
/**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'll tell you whether the number is higher or lower.

 You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0):

 -1 : My number is lower
 1 : My number is higher
 0 : Congrats! You got it!
 Example:
 n = 10, I pick 6.

 Return 6.*/
public class _374 {
    /**The core problem/trouble to solve this problem is to figure out the problem description:
     * this API: guess(int num) means to take your guess num and let you know if your guessed num is bigger or smaller than the answer.
     * That's why if num > target, it returns -1 which means the target is smaller than your guess!!!*/

    public int guessNumber(int n) {
        int left = 1;
        int right = n;
        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            int g = guess(mid);
            if (g == 0) {
                return mid;
            } else if (g > 0) {
                left = mid;
            } else {
                right = mid;
            }
        }
        if (guess(left) == 0) {
            return left;
        }
        return right;
    }

    /**
     * This is a fake guess method that I wrote just to compile/test, I'll have to change it to another number other than 6 based on the number to be found.
     */
    private int guess(int num) {
        if (num > 6) {
            return -1;
        } else if (num < 6) {
            return 1;
        } else {
            return 0;
        }
    }

    public static void main(String... strings) {
        _374 test = new _374();
        System.out.println(test.guessNumber(10));
    }

}

@@ -28,19 +28,61 @@
 utions;%0A
+%0A
 /**
+%0A * 374. Guess Number Higher or Lower%0A * 
 We are p
@@ -128,17 +128,18 @@
 ollows:%0A
-%0A
+ *
  I pick 
@@ -201,17 +201,18 @@
 picked.%0A
-%0A
+ *
  Every t
@@ -285,17 +285,18 @@
  lower.%0A
-%0A
+ *
  You cal
@@ -485,17 +485,16 @@
 pick 6.%0A
-%0A
  Return 
@@ -495,16 +495,19 @@
 eturn 6.
+%0A%0A 
 */%0Apubli
@@ -1826,16 +1826,18 @@
 umber(10
+00
 ));%0A    

refactor 374