</>

33. Search in Rotated Sorted Array

Medium NeetCode 150
Binary Search Time O(log n) Space O(1)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Binary Search
Time
O(log n)
Space
O(1)

Standard Solution (C++)

SummaryGiven an integer array that was originally sorted in ascending order and then rotated at some unknown pivot, find the index of a given target value, or return -1 if it is not present. All values in the array are distinct.

Approach
  • Use binary search, but at each step determine which half of the current window is contiguously sorted.
  • If nums[left] <= nums[mid], the left half is sorted: check whether the target falls within that range and narrow accordingly; otherwise search the right half.
  • Otherwise the right half must be sorted: apply the symmetric check for the right range.
  • This eliminates half the search space each iteration despite the rotation, keeping the algorithm O(log n).
#include <vector>
using namespace std;

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int lo = 0, hi = static_cast<int>(nums.size()) - 1;

        while (lo <= hi) {
            int mid = lo + (hi - lo) / 2;

            if (nums[mid] == target) return mid;

            // Determine which half is contiguously sorted.
            if (nums[lo] <= nums[mid]) {
                // Left half [lo..mid] is sorted.
                if (nums[lo] <= target && target < nums[mid]) {
                    hi = mid - 1;   // target lies in the sorted left half
                } else {
                    lo = mid + 1;   // search the (possibly rotated) right half
                }
            } else {
                // Right half [mid..hi] is sorted.
                if (nums[mid] < target && target <= nums[hi]) {
                    lo = mid + 1;   // target lies in the sorted right half
                } else {
                    hi = mid - 1;   // search the (possibly rotated) left half
                }
            }
        }

        return -1;
    }
};
Time O(log n) Space O(1)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int left=0, right=nums.size()-1;

        while(left<=right){
            int mid=(left+right)/2;

            if(nums[mid]==target){
                return mid;
            }
            else if(nums[left]<=nums[mid]){ // left side sorted
                if(nums[left]<=target && nums[mid]>target)
                    right=mid-1;
                else
                    left=mid+1;
            }
            else{ // right side sorted
                if(nums[mid]<target && nums[right]>=target)
                    left=mid+1;
                else
                    right=mid-1;
            }
        }

        return -1;
    }
};

// Time Complexity: O(log n)
// Space Complexity: O(1)
Time O(log n) Space O(1)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern