Search in Rotated Sorted Array

Write a code to search in rotated sorted array.

Given a sorted array and a target value.  Suppose this sorted array is rotated any number of times.

Write a code to search a target value in a sorted and rotated array. Return the index of a target value. If the target value is not found then return -1.

NOTE: The array only contains distinct values.

For example:

Input: {4, 5, 6, 7, -1, 0, 1, 2},  target = 0

Output: 5

In the above example, we have to search 0 in the rotated array. The element 0 is present at 5th index.

The array is rotated which makes this problem tricky. Our task is to solve this problem efficiently.

There are multiple approaches we can use to solve this problem. Also, I have explained all the approaches and their java code through the video tutorial. The link to that video tutorial is present at the end of this post.

Search Element in a Rotated Sorted Array using Linear Search

The easiest approach is to search the target value using linear search. Traverse an array and compare each array element with the target value. If an array element is equal to the target value then return its index, else return -1.

The time complexity of this approach is O(n) and its space complexity is O(1).

It is the easiest way to approach this problem. Can we improve its time complexity? Is there any other approach we can use to solve this problem efficiently?

We can solve this problem in O(logn) time complexity using Binary Search.

Binary Search using Recursion

Find first and last position of element in sorted array

Programming questions on binary search

Programming video tutorials

Search in Rotated Sorted Array

Search in Rotated Sorted Array using Binary Search

We can use a binary search to solve this problem in O(logn) time complexity. How we can use binary search in a sorted and rotated array?

The interesting property of a sorted and rotated array is that when we divide the array into two halves. At least, one of the two halves is always sorted. If mid happens to the point of rotation then both the (right and left) halves will be sorted.

By using this observation we can use binary search to solve this problem. Here are the following steps.

i) In a binary search, we first calculate the mid. The next step is to compare the value present at the mid index to the target value. If both are equal then return the mid index.

ii) If the value present at the mid index is not equal to the target. Then compare the value present at mid index is greater than the value present at start index. If this is the case, It means the values present in this range are sorted. Then perform the following steps.

iii) Else for arr[mid] to arr[end] perform following steps.

Let’s write its java code.

Search in Rotated Sorted Array LeetCode Solution

Rotated Sorted Array Search InterviewBit Solution

A similar problem is present in Interviewbit.

Search an element in a sorted rotated array video tutorial

Tagged , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.