Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created May 15, 2026 21:26
Show Gist options
  • Select an option

  • Save tatsuyax25/bd1e6872444c0811270414082bf7aae3 to your computer and use it in GitHub Desktop.

Select an option

Save tatsuyax25/bd1e6872444c0811270414082bf7aae3 to your computer and use it in GitHub Desktop.
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: [4,5,6,7,0,1,2] if it was rotated 4 times. [0,1,2,4,5,6,7] if it was rotated 7 times. Notice that ro
/**
* @param {number[]} nums
* @return {number}
*/
var findMin = function(nums) {
let left = 0;
let right = nums.length - 1;
while (left < right) {
const mid = Math.floor((left + right) / 2);
// If mid element is greater than the rightmost,
// the minimum is in the right half.
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
// Otherwise, the minimum is in the left half (including mid)
right = mid;
}
}
return nums[left];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment