Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Guess Number Higher or Lower problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Guess Number Higher or Lower problem solution, 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 3 possible results:

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

Return the number that I picked.

Leetcode Guess Number Higher or Lower problem solution

Leetcode Guess Number Higher or Lower problem solution in Python.

class Solution(object):
    def guessNumber(self, n):
        """
        :type n: int
        :rtype: int
        """
        left,right = 1,n
        while left<=right:
            mid = (left+right)/2
            gues = guess(mid)
            if gues == 0:
                return mid
            if gues == -1:
                right = mid -1
            if gues == 1:
                left = mid+1
        return -1

Guess Number Higher or Lower problem solution in Java.

public class Solution extends GuessGame {
    public int guessNumber(int n) {
        int left = 1;
        int right = n;
        while(left < right){
            int myPick = left + (right - left )/ 2;
            if(guess(myPick) == 0) return myPick;
            else if(guess(myPick) == 1){
                left = myPick + 1;
            }
            else{
                right = myPick - 1;
            }
        }
        return left;
    }
}

Problem solution in C++.

int guess(int num);

class Solution {
public:
    int guessNumber(int n) {
        if (guess(n) == 0)
            return n;
        int i = 1;
        int j = n;
        while (i < j) {
            int mid = i + (j - i) / 2;
            int tmp = guess(mid);
            if (tmp == 0)
                return mid;
            else if (tmp == 1)
                i = mid + 1;
            else
                j = mid - 1;
        }
        return i;
    }
};

Problem solution in C.

int guessNumber(int n){
    int l = 1, h = n;
    
    while(l <= h){
        int mid = l + (h-l)/2;
        
        if(guess(mid) == 0) return mid;
        else if(guess(mid) == 1){
            l = mid+1;
            mid = l + (h-l)/2;
        }
        else{
            h = mid-1;
            mid = l + (h-l)/2;
        }
    }
    return -1;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes