โ— LIVE
OpenAI releases GPT-5 APIIndia AI startup raises $120MBitcoin ETF hits record inflowsMeta Llama 4 benchmarks leakedOpenAI releases GPT-5 APIIndia AI startup raises $120MBitcoin ETF hits record inflowsMeta Llama 4 benchmarks leaked
๐Ÿ“… Sat, 21 Mar, 2026โœˆ๏ธ Telegram
AiFeed24

AI & Tech News

๐Ÿ”
โœˆ๏ธ Follow
๐Ÿ Home๐Ÿค–AI๐Ÿ’ปTech๐Ÿš€Startupsโ‚ฟCrypto๐Ÿ”’Security๐Ÿ‡ฎ๐Ÿ‡ณIndiaโ˜๏ธCloud๐Ÿ”ฅDeals
โœˆ๏ธ News Channel๐Ÿ›’ Deals Channel
Home/Cloud & DevOps/Guess Number Higher or Lower
โ˜๏ธCloud & DevOps

Guess Number Higher or Lower

Introduction This problem is a classic example of using Binary Search to efficiently find a value within a given range. Instead of checking every number one by one, we reduce the search space by half at each step. Problem Statement We are given a number between 1 and n. We need to guess the number u

โšกQuick SummaryAI generating...
C

Christina Sharon S

๐Ÿ“… Mar 21, 2026ยทโฑ 3 min readยทDev.to โ†—
โœˆ๏ธ Telegram๐• TweetWhatsApp
๐Ÿ“ก

Original Source

Dev.to

https://dev.to/christina_sharons_2b3205/guess-number-higher-or-lower-pbp
Read Full โ†—

Introduction

This problem is a classic example of using Binary Search to efficiently find a value within a given range.

Instead of checking every number one by one, we reduce the search space by half at each step.

Problem Statement

We are given a number between 1 and n. We need to guess the number using an API:

guess(num)

The API returns:

  • -1 โ†’ Your guess is higher than the number
  • 1 โ†’ Your guess is lower than the number
  • 0 โ†’ Your guess is correct

Our task is to find the correct number.

Example 1:

Input:

n = 10, pick = 6

Output:

6

Key Idea

Instead of guessing randomly, we use Binary Search:

  • Start with the range 1 to n
  • Pick the middle value
  • Use the API result to decide:

    • Search left half
    • Or search right half

Approach

  1. Initialize:
  • low = 1
  • high = n
  1. While low <= high:
  • Find middle: mid = (low + high) // 2
  • Call guess(mid)
  1. Based on result:
  • 0 โ†’ return mid
  • -1 โ†’ search left (high = mid - 1)
  • 1 โ†’ search right (low = mid + 1)

Python Implementation

# The guess API is already defined
# def guess(num): ...

def guessNumber(n):
    low = 1
    high = n

    while low <= high:
        mid = (low + high) // 2
        result = guess(mid)

        if result == 0:
            return mid
        elif result == -1:
            high = mid - 1
        else:
            low = mid + 1

Step-by-Step Example

For:

n = 10, pick = 6
  • mid = 5 โ†’ too low โ†’ go right
  • mid = 8 โ†’ too high โ†’ go left
  • mid = 6 โ†’ correct

Answer: 6

Why Binary Search Works Here

  • The range is sorted (1 to n)
  • Each guess eliminates half of the possibilities
  • Much faster than linear search

Key Points

  • Always use binary search when:

    • Data is sorted
    • You need to minimize operations
  • Reduces time significantly

  • Very common interview concept

Conclusion

The Guess Number problem is a perfect example of how Binary Search can optimize searching. Instead of checking every number, we intelligently narrow down the range, making the solution highly efficient.

Mastering this concept is essential for solving many advanced problems involving searching and optimization.

Tags:#cloud#dev.to

Found this useful? Share it!

โœˆ๏ธ Telegram๐• TweetWhatsApp

Read the Full Story

Continue reading on Dev.to

Visit Dev.to โ†—

Related Stories

โ˜๏ธ
โ˜๏ธCloud & DevOps

Majority Element

about 2 hours ago

โ˜๏ธ
โ˜๏ธCloud & DevOps

Building a SQL Tokenizer and Formatter From Scratch โ€” Supporting 6 Dialects

about 2 hours ago

โ˜๏ธ
โ˜๏ธCloud & DevOps

Markdown Knowledge Graph for Humans and Agents

about 2 hours ago

Moving Beyond Disk: How Redis Supercharges Your App Performance
โ˜๏ธCloud & DevOps

Moving Beyond Disk: How Redis Supercharges Your App Performance

about 2 hours ago

๐Ÿ“ก Source Details

Dev.to

๐Ÿ“… Mar 21, 2026

๐Ÿ• about 2 hours ago

โฑ 3 min read

๐Ÿ—‚ Cloud & DevOps

Read Original โ†—

Web Hosting

๐ŸŒ Hostinger โ€” 80% Off Hosting

Start your website for โ‚น69/mo. Free domain + SSL included.

Claim Deal โ†’

๐Ÿ“ฌ AiFeed24 Daily

Top 5 AI & tech stories every morning. Join 40,000+ readers.

โœฆ 40,218 subscribers ยท No spam, ever

Cloud Hosting

โ˜๏ธ Vultr โ€” $100 Free Credit

Deploy cloud servers in 25+ locations. From $2.50/mo. No contract.

Claim $100 Credit โ†’
AiFeed24

India's AI-powered tech news hub. Daily coverage of AI, startups, crypto and emerging technology.

โœˆ๏ธ๐Ÿ›’

Topics

Artificial IntelligenceStartups & VCCryptocurrencyCybersecurityCloud & DevOpsIndia Tech

Company

About AiFeed24Write For UsContact

Daily Digest

Top 5 AI stories every morning. 40,000+ readers.

No spam, ever.

ยฉ 2026 AiFeed24 Media.Affiliate Disclosure โ€” We earn commission on qualifying purchases at no extra cost to you.
PrivacyTermsCookies