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
Searching in a Rotated Sorted Array Using Modified Binary Search in Python
NewsProgramming Languages

Searching in a Rotated Sorted Array Using Modified Binary Search in Python

via Dev.to PythonSri Mahalakshmi4h ago

Problem Explanation You are given a sorted array nums that has been rotated at some unknown index . Your task is to 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 Input: nums = [1] , target = 0 Output: -1 Method Used: Modified Binary Search Idea Even though the array is rotated, one half is always sorted . So: Check which half is sorted Decide where the target lies Continue binary search Why This Method? Time complexity: O(log n) Efficient even after rotation No need to actually rotate back Python Code with Explanation class Solution : def search ( self , nums , target ): Defines the function. left = 0 right = len ( nums ) - 1 Initialize search range. while left <= right : Run binary search loop. mid = ( left + right ) // 2 Find middle index. if nums [ mid ] == target : return mid If target found, return index. if nums [ left ] <= nums [ mi

Continue reading on Dev.to Python

Opens in a new tab

Read Full Article
2 views

Related Articles

Give Your Phone a Huge (and Free) Upgrade by Switching to Another Keyboard
News

Give Your Phone a Huge (and Free) Upgrade by Switching to Another Keyboard

Wired • 3h ago

Title: February 2026: The Final Month for STON/USDT V2 Protection
News

Title: February 2026: The Final Month for STON/USDT V2 Protection

Medium Programming • 4h ago

12 Best Coffee Subscriptions (2026), Tested by Caffeine Hounds
News

12 Best Coffee Subscriptions (2026), Tested by Caffeine Hounds

Wired • 4h ago

Do Not Buy the MacBook Neo. Here’s Why.
News

Do Not Buy the MacBook Neo. Here’s Why.

Medium Programming • 4h ago

The End of an Era: How 15 Months of Impermanent Loss Protection Changed TON DeFi
News

The End of an Era: How 15 Months of Impermanent Loss Protection Changed TON DeFi

Medium Programming • 5h ago

Discover More Articles