Search in a Rotated Array | Practice | GeeksforGeeks

Given a sorted and rotated array A of N distinct elements which is rotated at some point, and given an element key. The task is to find the index of the given element key in the array A.
Example 1:
Input:
N = 9
A[] = {5, 6, 7, 8, 9, 10, 1, 2, 3}
key = 10
Output:
5
Explanation: 10 is found at index 5.
Example 2:
Input:
N = 4
A[] = {3, 5, 1, 2}
key = 6
Output:
-1
Explanation: There is no element that has value 6.
Your Task:
Complete the function search() which takes an array arr[] and start, end index of the array and the K as input parameters, and returns the answer.

Can you solve it in expected time complexity?
Expected Time Complexity: O(log N).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 107
0 ≤ A[i] ≤ 108
1 ≤ key ≤ 108
 
0
js997778812 hours ago
Why this JavaScript code giving TLE every time after passing 428 test cases? And also I have tried O(N) approach, that also gives same TLE.
 
class Solution {
        binarySearch(arr, low, high, key){
        let mid = Math.floor((low + high) / 2);
        while(low <= high){
            if(key === arr[mid]){
                return mid;
            }
            else if(key > arr[mid]){
                low = mid + 1;
            }else{
                high = mid - 1;
            }
            mid = Math.floor((low + high) / 2);
        }
        return -1;
    }
    pivot(arr, low, high){
        let mid = Math.floor((low + high) / 2);
        while(low < high){
            if(arr[mid] < arr[0]){
                high = mid;
            }else{
                low = mid + 1;
            }
            mid = Math.floor((low + high) / 2);
        }
        return mid;
    }
  search(arr, low, high, key) {
    //code here
    let pivot = this.pivot(arr, low, high);
    if(arr[pivot] <= key && key <= arr[high]){
        return this.binarySearch(arr, pivot, high, key);
    }else{
        return this.binarySearch(arr, 0, pivot - 1, key)
    }
  }
}
0
manjeetsachan12342 days ago
// simple c++ solution
class Solution{   public:   int search(int A[], int l, int h, int key){   //complete the function here   while(l<=h){       int mid=l+(h-l)/2;       if(A[mid]==key){           return mid;       }       if(A[l]<=A[mid]){       if(A[l]<=key && key<=A[mid]){           h=mid-1;       }       else{           l=mid+1;       }       }       else{           if(A[mid]<=key && key<=A[h]){               l=mid+1;           }           else{               h=mid-1;           }       }   }   return -1;   }}; 
-1
mayank180919992 days ago
    int search(int A[], int l, int h, int key){
    //complete the function here
         h=h+1;
         for(int i=0;i<h;i++){
             if(A[i]==key){
                 return i;
             }
         }
      return -1;
    }
-1
officialsurajpandey553 days ago
if first part is sorted than why this condition is required
if we previously check the middle element
 if(A[low] <= A[mid])
 if(A[low] <= A[mid]){
               if(A[low] <= key and A[mid] > key)
                   high = mid-1;   //key is persent in this part 
               else 
                   low = mid+1;
           }
0
20je01175 days ago
binary search:-
 
int search(int A[],int l,int h,int key){       int mid;       while(l<=h){           mid=(l+h)/2;           if((A[mid]>=A[0]) == (key>=A[0])){               if(A[mid]==key)return mid;              else if(key>A[mid])l=mid+1;              else h=mid-1;           }           else{               if(A[mid]>=A[0])l=mid+1;               else h=mid-1;           }       }       return  -1;   }
-9
lovemurari20181 week ago
//C++ easy solution
 
int ans = -1;              for(int i = 0; i < l+h; i++) {           if(key == A[i]) {               ans = i;               break;           }       }              return ans;
0
dharpranoy22551 week ago
Python Solution : 1.78/3.17
def search(self, A : list, l : int, h : int, key : int):       j=0       for i in range(h-1,0,-1):           if key==A[i]:               return i           if A[i-1]>A[i]:               j=i-1               break       i=0       while i<=j:           mid=(i+j)//2           if A[mid]==key:               return mid           if key>A[mid]:               i=mid+1           elif key<A[mid]:               j=mid-1       return -1
0
raje_1431 week ago

//simple solution in c++

int search(int arr[], int start, int end, int target){
    //complete the function here
        while(start<=end)
        {
            int mid=start+(end-start)/2;
            if(arr[mid] == target){
            return mid;
            }
            if(arr[mid]>=arr[start])
            {
                if(target>=arr[start] && target<=arr[mid]){
                    end=mid-1;
                }
                else{
                    start=mid+1;
                }
            }
            else{
                if(target<=arr[end] && target>=arr[mid]){
                    start=mid+1;
                }
                else{
                    end=mid-1;
                }
            }
        }
        return -1;
    }
0
jayprakashpandey47b1 week ago
int search(int A[], int l, int h, int key){
    //complete the function here
    int mid=(l+h)/2;
    if(A[mid]==key) return mid;
    if(l>=h) return -1;
    if(A[mid]<=A[l]){
        if(A[mid]<=key && A[h]>=key) l=mid+1;
        else h=mid-1;
    }else{
        if(A[mid]>=key && A[l]<=key) h=mid-1;
        else l=mid+1;
    }
    return search(A,l,h,key);
    }
    //binary search log(n)
-3
subhambhttachariya031 week ago
 int ans = -1;              for(int i = 0; i < l+h; i++) {           if(key == A[i]) {               ans = i;               break;           }       }              return ans;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.