Skip to content

Instantly share code, notes, and snippets.

@rohith2506
Created March 4, 2015 05:22
Show Gist options
  • Save rohith2506/ff00a373a7228d814087 to your computer and use it in GitHub Desktop.
Save rohith2506/ff00a373a7228d814087 to your computer and use it in GitHub Desktop.
Find maximum element in sorted array
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