DDSA
Advertisement

33. Search in Rotated Sorted Array

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

Approach

Binary search. Determine which half is sorted, then check whether the target lies in that half and narrow accordingly.

33.cs
C#
// Approach: Binary search. Determine which half is sorted, then check
// whether the target lies in that half and narrow accordingly.
// Time: O(log n) Space: O(1)

public class Solution
{
    public int Search(int[] nums, int target)
    {
        int n = nums.Length;
        int low = 0, high = n - 1, mid = 0;
        while (low <= high)
        {
            mid = (low + high) / 2;
            if (nums[mid] == target)
                return mid;
            if (nums[low] <= nums[mid])
            {
                if (nums[low] <= target && target <= nums[mid])
                    high = mid - 1;
                else
                    low = mid + 1;
            }
            else
            {
                if (nums[mid] <= target && target <= nums[high])
                    low = mid + 1;
                else
                    high = mid - 1;
            }
        }

        return -1;
    }
}
Advertisement
Was this solution helpful?