Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Search Rotated Sorted Array Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Search Rotated Sorted Array | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Practice Problem

You are given a list of unique integers which are sorted but rotated at some pivot. You are also given a target value and you have to find its index in the list. If it is not present in the list, return -1.

Naive Approach

A simple solution is to just iterate the array and find the index of the target value by comparing each element of the array with the target value.

Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Implementation

C++
int getElementIndex(vector<int> array, int target) {
   int indx = -1;
   for (int i = 0; i < array.size(); i++) {
       if (array[i] == target) {
           indx = i;
       }
   }
   return indx;
}
Java
class Solution {
   int getElementIndex(int[] array, int target) {
       int indx = -1;
       for (int i = 0; i < array.length; i++) {
           if (array[i] == target) {
               indx = i;
           }
       }
       return indx;
   }
}

Optimal Solution

The array can be treated as two different sorted arrays. All we need to do is to find the point from where the array needs to be broken down into two different arrays.

  • To find that point we can perform a binary search to find the only element which is greater than its next element.
  • After finding that point we can apply binary search in both parts of the array to find the index of the key.

Analysis

  • Time Complexity: O(log(n))
  • Space Complexity: O(1)

Implementation

C++
int findRotatedIndx (vector<int> arr, int low, int high) {
   if (high >= low) {
       int mid = (high + low) / 2;
       if (mid != 0 && arr[mid - 1] > arr[mid]) {
           return mid - 1;
       } else if (arr[0] >= arr[mid]) {
           return findRotatedIndx (arr, low, mid - 1);
       } else {
           return findRotatedIndx (arr, mid + 1, high);
       }
   }
   return - 1;
}
int binarySearch (vector<int> arr, int low, int high, int target) {
   if(high >= low) {
       int mid = (high + low) / 2;
       if (arr[mid] == target) {
           return mid;
       } else if(arr[mid] > target) {
           return binarySearch (arr, low, mid - 1, target);
       } else {
           return binarySearch (arr, mid + 1, high, target);
       }
   }
   return - 1;
}
int getElementIndex(vector<int> array, int target) {
   int n = array.size();
   int rotatedIndx = findRotatedIndx (array, 0, n - 1);
   if (rotatedIndx == -1) {
       return binarySearch (array, 0, n - 1, target);
   }
   if (array[0] <= target) {
       return binarySearch (array, 0, rotatedIndx, target);
   } else {
       return binarySearch (array, rotatedIndx + 1, n - 1, target);
   }
}
Java
class Solution {
   int findRotatedIndx (int[] arr, int low, int high) {
       if (high >= low) {
           int mid = (high + low) / 2;
           if (mid != 0 && arr[mid - 1] > arr[mid]) {
               return mid - 1;
           } else if (arr[0] >= arr[mid]) {
               return findRotatedIndx (arr, low, mid - 1);
           } else {
               return findRotatedIndx (arr, mid + 1, high);
           }
       }
       return -1;
   }
   int binarySearch (int[] arr, int low, int high, int target) {
       if(high >= low) {
           int mid = (high + low) / 2;
           if (arr[mid] == target) {
               return mid;
           } else if(arr[mid] > target) {
               return binarySearch (arr, low, mid - 1, target);
           } else {
               return binarySearch (arr, mid + 1, high, target);
           }
       }
       return - 1;
   }
   int getElementIndex(int[] array, int target) {
       int n = array.length;
       int rotatedIndx = findRotatedIndx (array, 0, n - 1);
       if (rotatedIndx == -1) {
           return binarySearch (array, 0, n - 1, target);
       }
       if (array[0] <= target) {
           return binarySearch (array, 0, rotatedIndx, target);
       } else {
           return binarySearch (array, rotatedIndx + 1, n - 1, target);
       }
   }
}
Related Content
Contains Element?
Insert Position in Sorted Array
Is Perfect Square
Matrix Search
Median of Row-wise Sorted Matrix
Negative numbers in sorted array
Next Greater Element In Sorted Array
Non-Repeating Element
Search Range
Square Root
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet