Created
March 4, 2015 05:22
-
-
Save rohith2506/ff00a373a7228d814087 to your computer and use it in GitHub Desktop.
Find maximum element in sorted array
This file contains hidden or 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
void find(vector<int> a, int low, int high){ | |
if(high == low+1 && a[low] <= a[high]) return a[high]; | |
if(high == low+1 && a[low] >= a[high]) return a[low]; | |
int mid = (low + high)/2; | |
if(a[low] < a[mid] && a[mid] > a[high]) return a[mid]; | |
if(a[mid-1] < a[mid] && a[mid] < a[mid+1]) return find(a,low,mid-1); | |
else return find(a,mid+1, high); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment