33. Search in Rotated Sorted Array

Problem

Intuition

Since the array is rotated, we cannot eliminate left or right half just like that. We need to check which half we need to eliminate.

  • To check if the first half is sorted

  • In other cases, it means right half is sorted

Once we know which part of the array we need to search, we check if the given target is in range

  • If the target is in left part, then search towards left, else right

  • If target is in right part, then search towards right, else left

Time Complexity

O(log n)

Space Complexity

O(n)

Solution

Last updated