Skip to content

Instantly share code, notes, and snippets.

@HDegano
Last active August 29, 2015 14:07
Show Gist options
  • Save HDegano/d9d5da50589dc83a7283 to your computer and use it in GitHub Desktop.
Save HDegano/d9d5da50589dc83a7283 to your computer and use it in GitHub Desktop.
Find the minimum in a sorted and rotated Array
//Assumes there are no duplicate
//O(log n)
public int FindMinRotatedArray(int[] ar){
int n = ar.Length;
int lo = 0;
int hi = n - 1;
while(lo <= hi){
if(ar[lo] <= ar[hi]) // (sub) array is already sorted, yay!
return ar[lo];
int mid = lo + (hi - lo)/2; //prevent int overflow, assumes all are positive
int next = (mid + 1) % n; //modulus is needed if mid is the start/end of the array
int prev = (mid + n - 1) % n;
//check if mid is the min. Both it's previous and next are higher
if((ar[mid] <=ar[prev])&&(ar[mid] <=ar[next]))
return ar[mid];
//figure out where is the dip
if(ar[mid] <= ar[lo])
hi = mid - 1; //the dip is in the half left side
else
lo = mid + 1;
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment