@@ -0,0 +1,1579 @@
+package com.freetymekiyan.algorithms.level.easy;%0A%0Aimport java.util.Random;%0A%0A/**%0A * We are playing the Guess Game. The game is as follows:%0A * %3Cp%3E%0A * I pick a number from 1 to n. You have to guess which number I picked.%0A * %3Cp%3E%0A * Every time you guess wrong, I'll tell you whether the number is higher or lower.%0A * %3Cp%3E%0A * You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0):%0A * %3Cp%3E%0A * -1 : My number is lower%0A * 1 : My number is higher%0A * 0 : Congrats! You got it!%0A * Example:%0A * n = 10, I pick 6.%0A * %3Cp%3E%0A * Return 6.%0A * %3Cp%3E%0A * Tags: Binary Search%0A * Similar Problems: (E) First Bad Version (M) Guess Number Higher or Lower II%0A */%0Apublic class GuessNumberHigherOrLower %7B%0A%0A    private int k;%0A%0A    public int guessNumber(int n) %7B%0A        k = new Random().nextInt(n); // Randomly generate a number to guess%0A        int l = 1;%0A        int h = n;%0A        while (l %3C= h) %7B%0A            int m = l + (h - l) / 2;%0A            int res = guess(m);%0A            if (res == 0) %7B%0A                return m;%0A            %7D else if (res %3E 0) %7B%0A                l = m + 1;%0A            %7D else %7B%0A                h = m - 1;%0A            %7D%0A        %7D%0A        return l;%0A    %7D%0A%0A    /**%0A     * The guess API is defined in the parent class GuessGame.%0A     *%0A     * @param num, your guess%0A     * @return -1 if my number is lower, 1 if my number is higher, otherwise return 0%0A     * int guess(int num);%0A     */%0A    private int guess(int num) %7B%0A        if (num == k) %7B%0A            return 0;%0A        %7D else if (num %3C k) %7B%0A            return 1;%0A        %7D%0A        return -1;%0A    %7D%0A%7D%0A

add Guess Number Higher or Lower