Skip to content

Commit

Permalink
Added solution - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
sainikcodes24x7 committed Aug 9, 2023
1 parent 3fa80ff commit d677a86
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Search in a Rotated Array - GFG/search-in-a-rotated-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;

// } Driver Code Ends
class Solution{
public:
int search(int arr[], int l, int h, int key){
// l: The starting index
// h: The ending index, you have to search the key in this range
int s=l,e=h;
while(s<=e){
int m=(s+e)/2;
if(arr[m]==key){
return m;
}
else if(arr[m]>=arr[s]){
if(key>=arr[s] and key<arr[m]){
e=m-1;
}
else{
s=m+1;
}
}
else{
if(key>arr[m] and key<=arr[e]){
s=m+1;
}
else{
e=m-1;
}
}
}
return -1;
//complete the function here
}
};

//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int A[n];
for(int i = 0; i < n; i++)
cin >> A[i];
int key;
cin >> key;
Solution ob;
cout << ob.search(A, 0, n - 1, key) << endl;
}
return 0;
}
// } Driver Code Ends

0 comments on commit d677a86

Please sign in to comment.