
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.
- The original array was [1,2,3,4,5] which was sorted in ascending order.
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.
- The original sorted array was [0,1,2,4,5,6,7].
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)
Editorial
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. |
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