File tree
Expand file treeCollapse file tree1 file changed
+50
-0
lines changed Expand file treeCollapse file tree1 file changed
+50
-0
lines changed Original file line number | Diff line number | Diff line change |
---|
|
| 1 | +#include<iostream> |
| 2 | +#include<conio.h> |
| 3 | +#include<stdlib.h> |
| 4 | +#include<bits/stdc++.h> |
| 5 | + |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +int hello() |
| 9 | +{ |
| 10 | +vector<int>arr(100); |
| 11 | +int n; |
| 12 | +cin>>n; |
| 13 | +for(int i=0;i<n;i++) |
| 14 | +{ |
| 15 | +cin>>arr[i]; |
| 16 | +} |
| 17 | +int target; |
| 18 | +cin>>target; |
| 19 | +int low=0,high=n-1; |
| 20 | +while(low<=high) |
| 21 | +{ |
| 22 | +int mid=low+(high-low)/2; |
| 23 | +if(arr[mid]==target)return mid; |
| 24 | +if(arr[mid]>=arr[low]) |
| 25 | +{ |
| 26 | +if(target>=arr[low]&&target<arr[mid]) |
| 27 | +high=mid-1; |
| 28 | +else |
| 29 | +low=mid+1; |
| 30 | +} |
| 31 | +else |
| 32 | +{ |
| 33 | +if(target>arr[mid]&&target<=arr[high]) |
| 34 | +low=mid+1; |
| 35 | +else |
| 36 | +high=mid-1; |
| 37 | +} |
| 38 | +} |
| 39 | + |
| 40 | +} |
| 41 | + |
| 42 | +main() |
| 43 | +{ |
| 44 | +cout<<hello(); |
| 45 | + |
| 46 | + |
| 47 | +return 0; |
| 48 | +} |
| 49 | + |
| 50 | + |
You can’t perform that action at this time.
0 commit comments