You are given a rotated sorted array with unique elements.
Your task is to return the minimum value in this array.
A rotated sorted array is formed by taking a sorted array and rotating (shifting) it some number of times.
Example:
Original sorted array: [0, 1, 2, 4, 5, 6, 7]
After rotation: [4, 5, 6, 7, 0, 1, 2]
Minimum element here is 0.
Understanding the Problem
The array is sorted but rotated.
This means there is a pivot point where the array was cut and shifted.
Key property:
Left half is sorted
Right half is sorted
Only one point breaks the sorted order: the pivot
Minimum element is always at or near this pivot.
We must solve it in O(log n), so binary search is required.
Approach (Explained in Simple Language)
Binary search helps locate the minimum by comparing mid values with the rightmost element.
1. Use two pointers: left and right
Start with left = 0 and right = n−1.
2. Check mid element
mid = (left + right) / 2
Now compare:
If nums[mid] > nums[right],
the min must be in the right half (because rotation pivot lies there).
So move left = mid + 1.
Else
the min is in the left half including mid.
So move right = mid.
3. When left meets right, that position holds the minimum
Binary search ensures logarithmic time.
This works because rotated sorted arrays maintain partial ordering.
Java Code
public class FindMinimumInRotatedSortedArray {
public int findMin(int[] nums) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left];
}
public static void main(String[] args) {
FindMinimumInRotatedSortedArray solution = new FindMinimumInRotatedSortedArray();
int[] nums = {4,5,6,7,0,1,2};
System.out.println(solution.findMin(nums));
}
}
Dry Run
Input:
nums = [4, 5, 6, 7, 0, 1, 2]
left = 0
right = 6
Step 1
mid = (0 + 6) / 2 = 3
nums[mid] = 7
nums[right] = 2
7 > 2 → min is on right side
left = mid + 1 = 4
Step 2
left = 4, right = 6
mid = (4 + 6) / 2 = 5
nums[mid] = 1
nums[right] = 2
1 < 2 → min is on left half including mid
right = mid = 5
Step 3
left = 4, right = 5
mid = (4 + 5) / 2 = 4
nums[mid] = 0
nums[right] = 1
0 < 1 → min is on left half including mid
right = mid = 4
Step 4
left = 4, right = 4
Loop ends.
Minimum = nums[left] = 0
Why This Approach Works
The key observation is that the sorted property still exists in the rotated array but is broken at one pivot.
By comparing mid with right:
If nums[mid] > nums[right], mid is in the left-sorted portion, and the pivot (and minimum) lies on the right.
If nums[mid] <= nums[right], mid is in the right-sorted portion, and the minimum is at or before mid.
This ensures we narrow the search space correctly.
Time complexity: O(log n)
Space: O(1)
