Guess the Number Higher or Lower

In this task, I worked on finding a hidden number within a given range using an efficient approach. Instead of checking every number one by one, I used binary search to reduce the number of guesses...

By · · 1 min read
Guess the Number Higher or Lower

Source: DEV Community

In this task, I worked on finding a hidden number within a given range using an efficient approach. Instead of checking every number one by one, I used binary search to reduce the number of guesses. What I Did I created a function guessNumber that tries to find a number between 1 and n. There is a helper function called guess(): It returns 0 if the guess is correct It returns -1 if the guessed number is too high It returns 1 if the guessed number is too low How I Solved It I used two variables: low starting from 1 high starting from n Then I repeatedly found the middle value (mid) of the range. At each step: If the guess is correct, I return the number If the guess is too high, I move the high pointer down If the guess is too low, I move the low pointer up This keeps reducing the search space until the correct number is found. Code class Solution: def guessNumber(self, n: int) -> int: low = 1 high = n while low <= high: mid = (low + high) // 2 res = guess(mid) if res == 0: return

Related Posts

Similar Topics

#data science (350)#programming (251)#machine learning (249)#artificial intelligence (125)#for (113)#learn (107)#ai (88)#python (61)#optimization (45)#editors pick (57)#tutorial (47)#hands on tutorials (39)#webdev (46)#deep dives (42)#math (33)#mathematics (26)#productivity (38)#instrument (15)#software engineering (17)#chatgpt (13)

Trending on ShareHub

  1. Understanding Modern JavaScript Frameworks in 2026
    by Alex Chen · Feb 12, 2026 · 0 likes
  2. The System Design Primer
    by Sarah Kim · Feb 12, 2026 · 0 likes
  3. Just shipped my first open-source project!
    by Alex Chen · Feb 12, 2026 · 0 likes
  4. OpenAI Blog
    by Sarah Kim · Feb 12, 2026 · 0 likes
  5. Building Accessible Web Applications: A Practical Guide
    by Alex Chen · Feb 12, 2026 · 0 likes
  6. Rapper Lil Poppa dead at 25, days after releasing new music
    Rapper Lil Poppa dead at 25, days after releasing new music
    by Anonymous User · Feb 19, 2026 · 0 likes
  7. write-for-us
    by Volt Raven · Mar 7, 2026 · 0 likes
  8. Before the Coffee Gets Cold: Heartfelt Story of Time Travel and Second Chances
    Before the Coffee Gets Cold: Heartfelt Story of Time Travel and Second Chances
    by Anonymous User · Feb 12, 2026 · 0 likes
    #coffee gets cold #the #time travel
  9. Best DoorDash Promo Code Reddit Finds for Top Discounts
    Best DoorDash Promo Code Reddit Finds for Top Discounts
    by Anonymous User · Feb 12, 2026 · 0 likes
    #doordash #promo #reddit
  10. Premium SEO Services That Boost Rankings & Revenue | VirtualSEO.Expert
    by Anonymous User · Feb 12, 2026 · 0 likes
  11. NBC under fire for commentary about Team USA women's hockey team
    NBC under fire for commentary about Team USA women's hockey team
    by Anonymous User · Feb 18, 2026 · 0 likes
  12. Where to Watch The Nanny: Streaming and Online Viewing Options
    Where to Watch The Nanny: Streaming and Online Viewing Options
    by Anonymous User · Feb 12, 2026 · 0 likes
    #streaming #the nanny #where
  13. How Much Is Kindle Unlimited? Subscription Cost and Plan Details
    How Much Is Kindle Unlimited? Subscription Cost and Plan Details
    by Anonymous User · Feb 12, 2026 · 0 likes
    #kindle unlimited #subscription #unlimited
  14. Russian skater facing backlash for comment about Amber Glenn
    Russian skater facing backlash for comment about Amber Glenn
    by Anonymous User · Feb 18, 2026 · 0 likes
  15. Google News
    Google News
    by Anonymous User · Feb 18, 2026 · 0 likes

Latest on ShareHub

Browse Topics

#artificial intelligence (31582)#data science (24018)#ai (17291)#generative ai (15034)#crypto (15017)#machine learning (14681)#bitcoin (14274)#featured (13571)#news & insights (13064)#crypto news (11094)

Around the Network