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
Search in Rotated Sorted Array Using Binary Search
NewsWeb Development

Search in Rotated Sorted Array Using Binary Search

via Dev.to TutorialSandhya Steffy M3h ago

Problem Statement: Given a sorted array that has been rotated at an unknown index, find the index of a target element. If the target is not present, return -1. Example: Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4 Input: nums = [4,5,6,7,0,1,2], target = 3 Output: -1 Approach: We use a modified Binary Search. In a rotated sorted array, at least one half is always sorted. By checking which half is sorted, we can decide where the target might lie and reduce the search space. Code: def search(nums, target): low = 0 high = len(nums) - 1 while low <= high: mid = (low + high) // 2 if nums[mid] == target: return mid if nums[low] <= nums[mid]: if nums[low] <= target < nums[mid]: high = mid - 1 else: low = mid + 1 else: if nums[mid] < target <= nums[high]: low = mid + 1 else: high = mid - 1 return -1 Explanation: The algorithm first checks the middle element. Then it determines whether the left half or right half is sorted. Based on where the target lies, it continues the search in the co

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
2 views

Related Articles

Beat Paxos
News

Beat Paxos

Lobsters • 3h ago

Your Code is 80% Garbage (And So Is Mine)
News

Your Code is 80% Garbage (And So Is Mine)

Medium Programming • 3h ago

What are you doing this weekend?
News

What are you doing this weekend?

Lobsters • 3h ago

&#8216;Work from home,&#8217; encourages the world&#8217;s energy watchdog
News

&#8216;Work from home,&#8217; encourages the world&#8217;s energy watchdog

The Verge • 4h ago

Best Kids' Bikes (2026): Woom, Prevelo, Guardian, and More
News

Best Kids' Bikes (2026): Woom, Prevelo, Guardian, and More

Wired • 4h ago

Discover More Articles