-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fa80ff
commit d677a86
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
Search in a Rotated Array - GFG/search-in-a-rotated-array.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |