Tutorialspoint
Problem
Solution
Submissions

Minimum in Rotated Sorted Array

Certification: Intermediate Level Accuracy: 0% Submissions: 0 Points: 10

Write a JavaScript program to find the minimum element in a rotated sorted array. The array was originally sorted in ascending order, then rotated at some pivot unknown to you beforehand. All elements in the array are unique, and you must solve this in O(log n) time complexity.

Example 1
  • Input: nums = [3,4,5,1,2]
  • Output: 1
  • Explanation:
    • The original array was [1,2,3,4,5] which was sorted in ascending order.
    • The array was rotated at pivot index 3, resulting in [3,4,5,1,2].
    • The minimum element is 1, which appears at index 3 in the rotated array.
    • Binary search can efficiently locate this minimum by comparing middle elements.
    • The minimum element is found where the rotation occurred.
Example 2
  • Input: nums = [4,5,6,7,0,1,2]
  • Output: 0
  • Explanation:
    • The original sorted array was [0,1,2,4,5,6,7].
    • After rotation at some pivot, it became [4,5,6,7,0,1,2].
    • The minimum element is 0, located at index 4.
    • Using binary search, we can eliminate half of the search space in each iteration.
    • The algorithm identifies the rotation point where the minimum element resides.
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
  • Time Complexity: O(log n)
  • Space Complexity: O(1)
ArraysAlgorithmsTech MahindraAdobe
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use binary search to achieve O(log n) time complexity
  • Compare the middle element with the rightmost element to determine which half contains the minimum
  • If nums[mid] > nums[right], the minimum is in the right half
  • If nums[mid] < nums[right], the minimum is in the left half (including mid)
  • Continue narrowing the search space until left and right pointers meet

Steps to solve by this approach:

 Step 1: Initialize left pointer to 0 and right pointer to the last index of the array

 Step 2: Use binary search by calculating the middle index as the average of left and right pointers
 Step 3: Compare the middle element with the rightmost element to determine which half is unsorted
 Step 4: If nums[mid] > nums[right], the rotation point (minimum) is in the right half, so move left = mid + 1
 Step 5: If nums[mid] <= nums[right], the minimum could be at mid or in the left half, so move right = mid
 Step 6: Continue the binary search until left and right pointers converge at the minimum element
 Step 7: Return the element at the converged position, which is guaranteed to be the minimum

Submitted Code :