Algorithm


Problem Name: 153. Find Minimum in Rotated Sorted Array

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 rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].

Given the sorted rotated array nums of unique elements, return the minimum element of this array.

You must write an algorithm that runs in O(log n) time.

 

Example 1:

Input: nums = [3,4,5,1,2]
Output: 1
Explanation: The original array was [1,2,3,4,5] rotated 3 times.

Example 2:

Input: nums = [4,5,6,7,0,1,2]
Output: 0
Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.

Example 3:

Input: nums = [11,13,15,17]
Output: 11
Explanation: The original array was [11,13,15,17] and it was rotated 4 times. 

 

Constraints:

  • n == nums.length
  • 1 <= n <= 5000
  • -5000 <= nums[i] <= 5000
  • All the integers of nums are unique.
  • nums is sorted and rotated between 1 and n times.

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
#include <assert.h>

int findMin(int *nums, int numsSize) {
    int l = 0;
    int r = numsSize - 1;
    while (l  <  r) {
        int m = l + (r - l) / 2;
        if (nums[l] > nums[m]) { /* right side is sorted */
            r = m;
        }
        else if (nums[r]  <  nums[m]) { /* left side is sorted */
            l = m + 1;
        }
        else { /* the sub-array is not rotated */
            r = m;
        }
    }
    return nums[l];
}

int main() {
    int nums[] = { 3, 1, 2 };

    assert(findMin(nums, sizeof(nums) / sizeof(nums[0])) == 1);

    printf("all tests passed!\n");

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [3,4,5,1,2]

Output

x
+
cmd
1

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int findMin(int[] nums) {
    if (nums.length == 1) {
      return nums[0];
    }
    int left = 0;
    int right = nums.length - 1;
    // Sorted but not rotated
    if (nums[right] > nums[left]) {
      return nums[left];
    }
    while (left  < = right) {
      int mid = (left + right) / 2;
      // mid + 1 is point of rotation
      if (nums[mid] > nums[mid + 1]) {
        return nums[mid + 1];
      }
      // mid is point of rotation
      if (nums[mid - 1] > nums[mid]) {
        return nums[mid];
      }
      if (nums[mid] > nums[0]) {
        left = mid + 1;
      } else {
        right = mid - 1;
      }
    }
    return Integer.MAX_VALUE;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [3,4,5,1,2]

Output

x
+
cmd
1

#3 Code Example with Javascript Programming

Code - Javascript Programming


const findMin = function (nums) {
  let low = 0,
    high = nums.length - 1
 
  while (low < high) {
    const mid = low + ((high - low) >> 1)
    if (nums[mid] <= nums[high]) high = mid
    else if (nums[mid] > nums[high]) low = mid + 1
  }

  return nums[low]
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [4,5,6,7,0,1,2]

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    findMin = min
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [4,5,6,7,0,1,2]

#5 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _0153_FindMinimumInRotatedSortedArray
    {
        public int FindMin(int[] nums)
        {
            if (nums.Length == 1) return nums[0];
            if (nums[nums.Length - 1] > nums[0]) return nums[0];

            int left = 0, right = nums.Length - 1;
            while (left  <  right)
            {
                var mid = left + (right - left) / 2;

                if (nums[mid] < nums[right]) right = mid;
                else left = mid + 1;
            }

            return nums[right];
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
nums = [11,13,15,17]

Output

x
+
cmd
11
Advertisements

Demonstration


Previous
#152 Leetcode Maximum Product Subarray Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#154 Leetcode Find Minimum in Rotated Sorted Array II Solution in C, C++, Java, JavaScript, Python, C# Leetcode