Back to articles
ROTATED SORTED ARRAY

ROTATED SORTED ARRAY

via Dev.to BeginnersAbinaya Dhanraj

How I Understood Search in a Rotated Sorted Array (LeetCode 33) When I first saw this problem, it looked tricky because the array is rotated, so normal binary search won’t always work. After analyzing it, I realized the key is to identify which half is sorted in each step. Problem Given a rotated sorted array nums and a target value, return its index if found, else return -1. A rotated array means it was originally sorted but then shifted at some pivot. Example: Python nums = [4,5,6,7,0,1,2] target = 0 Output: 4 Python nums = [4,5,6,7,0,1,2] target = 3 Output: -1 What I Noticed Instead of trying linear search, I noticed: Even after rotation, at least one half of the array is sorted We can use binary search logic on the sorted half Check if the target is in that half If yes, search there; if not, search the other half This keeps O(log n) efficiency. What Helped Me Breaking it into three key steps clarified the approach: Binary search loop: low <= high Check sorted half: If nums[low] <=

Continue reading on Dev.to Beginners

Opens in a new tab

Read Full Article
5 views

Related Articles