Suppose a sorted array is rotated on some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7
might become 4 5 6 7 0 1 2
).
Find the minimum element.
Assume no duplicate exists in the array.
Array A,
1) If A[MID] < A[low], indicate minimum between [mid, Low]. (Presence rotated)
2) if A[MID] > A[low], there are two cases:
A) A[mid] > A[high], minimum between [Mid, High]. (Presence rotated)
b) A[mid] < A[high], minimum between [Low, mid]. (Rotated not present)
Notice termination condition: A[mid] Less than left or right side or low = = Mid
1 intFindmin (vector<int> &num)2 {3 intLow =0, high = Num.size ()-1, Mid =0;4 while(Low <=High )5 {6Mid = (low + high)/2;7 if(Mid = =0|| Num[mid] < Num[mid-1]) && (Mid = = Num.size ()-1|| Num[mid] < Num[mid +1]))8 Break;9 if(Num[mid] <Num[low])Ten { OneHigh = mid-1; A } - Else if(Num[mid] >Num[low]) - { the if(Num[mid] >Num[high]) -Low = mid +1; - Else if(Num[mid] <Num[high]) -High = mid-1; + } - Else + { A returnmin (Num[low], Num[high]); at } - } - - returnNum[mid]; -}
Leetcode. Find Minimum in rotated Sorted Array