-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindmaxinbitonicarray.cpp
More file actions
47 lines (40 loc) · 1.28 KB
/
Copy pathfindmaxinbitonicarray.cpp
File metadata and controls
47 lines (40 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*Given an array of N distinct integers with the property that there exists
an index K (0 <= K <= N-1) such that input[0], ..., input[K] is and increasing sequence
and input[K], ..., input[N-1] is a decreasing sequence. Devise and algorithm to find K.
*/
/*Approach:
Try binary search
1)divide the array into 2 parts
if arr[mid-1]<arr[mid]<arr[mid+1]
k is towards the right.So make low=mid+1
if arr[mid-1]<arr[mid]>arr[mid+1]
mid is k
if arr[mid-1]>arr[mid]>arr[mid+1]
k is towards the left.So make high=mid-1
*/
#include<iostream>
using namespace std;
int findk(int arr[],int n){
int low=0;
int high=n-1;
int mid;
while(low<=high){
//if(low==high-1)
// return -1;
mid=low+(high-low)/2;
if((mid==0||arr[mid-1]<arr[mid])&&(mid!=n-1||arr[mid]>arr[mid+1]))
return mid;
else if((mid==0||arr[mid-1]<arr[mid])&&((mid!=n-1)||arr[mid]<arr[mid+1]))
low=mid+1;
else if((mid==0||arr[mid-1]>arr[mid])&&((mid!=n-1)||arr[mid]>arr[mid+1]))
high=mid-1;
else
return -1;
}
return -1;
}
int main(){
int arr[]={1,2,78,80,65,3};
int len=sizeof(arr)/sizeof(int);
cout<<findk(arr,len);
}