AlgoMaster Logo

Search in Rotated Sorted Array

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

Intuition:

The simplest approach to solve this problem is to iterate through the array and check each element if it matches the target. This brute force approach is straightforward but not efficient.

Code:

Intuition:

The array is rotated at some pivot, and this affects the normal binary search. The idea is to first find this pivot point, then determine in which of the two sub-arrays (from 0 to pivot, or pivot to n-1) the target resides, and perform a binary search in the appropriate sub-array.

Code:

Intuition:

Since the array is sorted and only rotated, a single pass binary search can be designed. By checking the sorted property, we can decide which half to continue the search on. This eliminates the need for a separate pivot finding step, thereby optimizing the binary search process.

Code: