FlareStart
HomeNewsHow ToSources
FlareStart

Where developers start their day. All the tech news & tutorials that matter, in one place.

Quick Links

  • Home
  • News
  • Tutorials
  • Sources
  • Privacy Policy

Connect

© 2026 FlareStart. All rights reserved.

Back to articles
Guess the Number Higher or Lower [Leetcode]
NewsProgramming Languages

Guess the Number Higher or Lower [Leetcode]

via Dev.to PythonSamantha Vincent2h ago

Guess Number Higher or Lower Problem You’re playing a game where a number is picked between 1 and n . You have to guess it using an API that tells you: -1 → your guess is too high 1 → your guess is too low 0 → correct guess Strategy This is basically a classic binary search problem. Instead of searching an array, you’re searching a range from 1 to n . At each step: Pick the middle number Use the API to check Narrow the range based on the response Code class Solution : def guessNumber ( self , n ): left , right = 1 , n while left <= right : mid = ( left + right ) // 2 res = guess ( mid ) if res == 0 : return mid elif res == - 1 : right = mid - 1 else : left = mid + 1 Key Lines Explained mid = (left + right) // 2 Choose the middle of the current range. res = guess(mid) Ask whether the guess is correct, too high, or too low. res == -1 Means the guess is too high → move left. res == 1 Means the guess is too low → move right. Why This Works Each guess eliminates half of the remaining range.

Continue reading on Dev.to Python

Opens in a new tab

Read Full Article
2 views

Related Articles

News

The Slow Collapse of MkDocs

Lobsters • 3h ago

News

All tests pass: a short story

Lobsters • 3h ago

The Emperor’s Monday
News

The Emperor’s Monday

Medium Programming • 3h ago

News

Hi

Medium Programming • 3h ago

TechCrunch Mobility: Uber everywhere, all at once
News

TechCrunch Mobility: Uber everywhere, all at once

TechCrunch • 3h ago

Discover More Articles