Search In Rotated Sorted Array — Asked in Google, Amazon, Apple Interviews

Problem Statement :

Aahad and Harshit always have fun by solving problems. Harshit took a sorted array and rotated it clockwise by an unknown amount. For example, he took a sorted array = [1, 2, 3, 4, 5] and if he rotates it by 2, then the array becomes: [4, 5, 1, 2, 3].

After rotating a sorted array, Aahad needs to answer Q queries asked by Harshit, each of them is described by one integer Q[i] which Harshit wanted him to search in the array. For each query, if he founds it, he had to shout the index of the number, otherwise, he had to shout -1.

The first line of input contains the size of the array: N

The second line contains N single space-separated integers: A[i].

The third line of input contains the number of queries: Q

The next Q lines of input contain: the number which Harshit wants Aahad to search: Q[i]

Print the index of the number if found, otherwise -1.

Sample Input:

Sample Output:

Explanation of given Test Cases :

Approach :

Binary Search Approach

Before we discuss the algorithm, there’s an interesting property about sorted and rotated arrays that must be noted.

If we divide the array into two halves, at least one of the two halves will always be sorted.

Let’s consider the array: [5, 6, 7, 1, 2, 3, 4]

Mid-index is calculated as (startIndex + endIndex) / 2 or start + (endIndex — startIndex) / 2.

The mid-index for the above-depicted list/array will be 3.

The value at the mid-index is 1. If we have a closer look at three values, that are, value at start, end, and mid then we can deduce which the subarray is sorted or not.

Since the value at the mid-index is less than the value at the start index, we clearly can say that the left subarray is not sorted or violates the property of a sorted array.

Similarly, we can deduce if the right subarray is sorted or not by comparing the values at mi-index and end index.

Now, once we know what part of the array is sorted, we can compare the value(key) to be searched in reference to the sorted subarray.

Finally, to put everything in points:

  1. Find the mid index

2. If the value(key) being searched for is at the mid index, then return the mid index.

3. Compare values at start index, end index, and mid-index:

  • If the left subarray is sorted, check if the value(key) to be searched lies in the range:
  • If it does, then search space reduces between [start, (mid-1)].
  • Otherwise, the search space reduces between [(mid + 1), end]
  • If the right subarray is sorted, check if the value(key) to be searched lies in the range:
  • If it does, then search space reduces between [(mid + 1), end].
  • Otherwise, the search space reduces between [start, (mid -1)]

4. Repeat from step-1 until the key is found.

5. Return -1 if never found.

Time complexity : O(log(N))
Space complexity : O(1)

Code :

Thanks for Reading

Placewit grows the best engineers by providing an interactive classroom experience and by helping them develop their skills and get placed in amazing companies.

Learn more at Placewit. Follow us on Instagram and Facebook for daily learning.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store