374. Guess Number Higher or Lower LeetCode Solution

In this guide, you will get 374. Guess Number Higher or Lower LeetCode Solution with the best time and space complexity. The solution to Guess Number Higher or Lower problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Guess Number Higher or Lower solution in C++
  4. Guess Number Higher or Lower solution in Java
  5. Guess Number Higher or Lower solution in Python
  6. Additional Resources
374. Guess Number Higher or Lower LeetCode Solution image

Problem Statement of Guess Number Higher or Lower

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 will tell you whether the number I picked is higher or lower than your guess.
You call a pre-defined API int guess(int num), which returns three possible results:

-1: Your guess is higher than the number I picked (i.e. num > pick).
1: Your guess is lower than the number I picked (i.e. num < pick).
0: your guess is equal to the number I picked (i.e. num == pick).

Return the number that I picked.

Example 1:

Input: n = 10, pick = 6
Output: 6

Example 2:

Input: n = 1, pick = 1
Output: 1

Example 3:

Input: n = 2, pick = 1
Output: 1

Constraints:

1 <= n <= 231 – 1
1 <= pick <= n

Complexity Analysis

  • Time Complexity: O(\log n)
  • Space Complexity: O(1)

374. Guess Number Higher or Lower LeetCode Solution in C++

/**
 * Forward declaration of guess API.
 * (The problem description is not clear, so I translate it into follows.)
 *
 * @param traget num
 *        guess num
 *
 * @return -1 if guess num >  target num
 *          0 if guess num == target num
 *          1 if guess num <  target num
 */

class Solution {
 public:
  int guessNumber(int n) {
    int l = 1;
    int r = n;

    // Find the first guess number that >= the target number
    while (l < r) {
      const int m = l + (r - l) / 2;
      if (guess(m) <= 0)  // -1, 0
        r = m;
      else
        l = m + 1;
    }

    return l;
  }
};
/* code provided by PROGIEZ */

374. Guess Number Higher or Lower LeetCode Solution in Java

/**
 * Forward declaration of guess API.
 * (The problem description is not clear, so I translate it into follows.)
 *
 * @param traget num
 *        guess num
 *
 * @return -1 if guess num >  target num
 *          0 if guess num == target num
 *          1 if guess num <  target num
 */

public class Solution extends GuessGame {
  public int guessNumber(int n) {
    int l = 1;
    int r = n;

    // Find the first guess number that >= the target number
    while (l < r) {
      final int m = l + (r - l) / 2;
      if (guess(m) <= 0) // -1, 0
        r = m;
      else
        l = m + 1;
    }

    return l;
  }
}
// code provided by PROGIEZ

374. Guess Number Higher or Lower LeetCode Solution in Python

# The guess API is already defined for you.
# @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
# def guess(num: int) -> int:

class Solution:
  def guessNumber(self, n: int) -> int:
    l = 1
    r = n

    # Find the first guess number that >= the target number
    while l < r:
      m = (l + r) // 2
      if guess(m) <= 0:  # -1, 0
        r = m
      else:
        l = m + 1

    return l
# code by PROGIEZ

Additional Resources

See also  974. Subarray Sums Divisible by K LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.