Keep and Share logo     Log In  |  Mobile View  |  Help  
 
Visiting
 
Select a Color
   
 












01.DS.02.02. Search a Rotated Array.html -- 1.9 meg
 Search a Rotated Array - Coderust: Hacking the Coding Interview
Log InJoin for free
Back To Module Home
Data Structures
0% completed
Further Reading
Mark Module as Completed
Ask a Question

Search a Rotated Array

Search for a given number in a sorted array that has been rotated by some arbitrary number.

Statement#

We’re given a sorted integer array, nums and an integer value, target. The array is rotated by some arbitrary number. Search the target in this array. If the target does not exist then return -1.

Example#

An original array before rotation is given below:

garray01234567891011121314151617181911020475963758899107120133155162176188199200210222

After rotating this array 6 times, it changes to:

garray01234567891011121314151617181917618819920021022211020475963758899107120133155162

Our task is to find the target in an already rotated array.

Sample input#

nums = [6, 7, 1, 2, 3, 4, 5]
target = 3

Expected output#

4

Try it yourself#

C++
Java
Python
JS
Ruby
Go
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <vector>
using namespace std;
int BinarySearchRotated(vector<int>& nums, int target) {
    // TODO: Write - Your - Code
    return -1;
}
#
#include <iostream> #include <vector> using namespace std; int BinarySearchRotated(vector<int>& nums, int target) { // TODO: Write - Your - Code return -1; }
Enter to Rename, Shift+Enter to Preview
Test
Need Hint?
Save
Reset

Solution 1: Iterative#

The solution is essentially a binary search but with some modifications. When we look at the array in the example, you may notice that at least one-half of the array is always sorted. We can use this property to our advantage. If the target lies within the sorted half of the array, our problem is a basic binary search. Otherwise, discard the sorted half and keep examining the unsorted half. Since we partition the array in half at each step, this gives us O(log n)O(log \space n)O(log n) runtime complexity.

Let’s assume we have the same array we saw above (rotated 6 times) and our target is 200200200.

Created with Fabric.js 3.6.6This is our initial setup.target: 20001234567891011121314151617181917618819920021022211020475963758899107120133155162
1 of 14
C++
Java
Python
JS
Ruby
Go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>
using namespace std;
int BinarySearchRotated(vector<int>& nums, int target) {
    int start = 0;
    int mid = 0;
    int end = nums.size() - 1;
    if (start > end) return -1;
    while (start <= end) {
        // Finding the mid using floor division
        mid = start + (end - start) / 2;
        // Target value is present at the middle of the numsay
        if (nums[mid] == target) return mid;
        // start to mid is sorted
        if (nums[start] <= nums[mid]) {
            if (nums[start] <= target && target < nums[mid]) {
                end = mid - 1;
            }
            else {
                start = mid + 1;
            }
        }
        // mid to end is sorted
        else {
#
#include <iostream> #include <vector> using namespace std; int BinarySearchRotated(vector<int>& nums, int target) { int start = 0; int mid = 0; int end = nums.size() - 1;
Enter to Rename, Shift+Enter to Preview
Run
Save
Reset

Time complexity#

The time complexity of this solution is logarithmic, O(log n)O(log \space n)O(log n).

Space complexity#

The space complexity of this solution is constant, O(1)O(1)O(1).

Solution 2: Recursive#

In this solution, we will use the recursive binary search approach to locate the target. Each recursive call considers half of the array passed to it.

C++
Java
Python
JS
Ruby
Go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>
using namespace std;
int BinarySearch(vector<int>& nums, int start, int end, int target) {
    if (start > end)
        return -1;
    
    // Finding the mid using floor division
    int mid = start + (end - start) / 2;
    if (nums[mid] == target)
        return mid;
    // start to mid is sorted
    if (nums[start] <= nums[mid]) {
        if (nums[start] <= target && target < nums[mid]) {
            return BinarySearch(nums, start, mid - 1, target);
        } else {
            return BinarySearch(nums, mid + 1, end, target);
        }
    }
    // mid to end is sorted
    else {
        if (nums[mid] < target && target <= nums[end]) {
            return BinarySearch(nums, mid + 1, end, target);
        } else {
            return BinarySearch(nums, start, mid - 1, target);
        }
#
#include <iostream> #include <vector> using namespace std; int BinarySearch(vector<int>& nums, int start, int end, int target) { if (start > end) return -1; // Finding the mid using floor division
Enter to Rename, Shift+Enter to Preview
Run
Save
Reset

Time complexity#

The time complexity of this solution is logarithmic, O(log n)O(log \space n)O(log n).

Space complexity#

The space complexity of this solution is logarithmic, O(log n)O(log \space n)O(log n).

Back
Find Maximum in Sliding Window
Next
Find the Smallest Common Number
Mark as Completed
Report an Issue


Creation date: Oct 7, 2022 7:32pm     Last modified date: Oct 7, 2022 7:32pm   Last visit date: Dec 6, 2024 6:00am
    Report Objectionable Content