DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Find Minimum in Rotated Sorted Array

Find Minimum in Rotated Sorted Array

/**
 * @param {number[]} nums
 * @return {number}
 */
var findMin = function(n) {
     let min = n[0];
    let l = 0;
    let r = n.length - 1;

    while (l <= r) {
        let m = Math.floor((l+r) / 2);
        if (n[m] < min) min = n[m]

        if (n[m] >= n[r]){
            l = m + 1;
        }else {
            r = m - 1;
        }
    }

    return min
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)