Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
680 B
3
Indexable
class Solution {
public:
    int findMin(vector<int>& arr) {
        int l=0,r=arr.size()-1;
        int ans=INT_MAX;
        while(l<=r){
            if(arr[l]<arr[r]){
                return min(ans,arr[l]); // the method stops working when our array gets sorted,our method only works till array remains rotated
            }
            int mid =l+(r-l)/2;
            ans=min(ans,arr[mid]);
            if(arr[mid] >= arr[l]){ // if we are in left sorted we go to right
                l=mid+1;
            }else{  //if we are in right sorted , we decrease our right sorted range
                r=mid-1;
            }
        }
        return ans;
    }
};