Find Minimum in Rotated Sorted Array in Java: Explanation & Practice
Find the minimum element in rotated array
Problem summary
Given a rotated sorted array of unique elements, find the minimum element. The original array was sorted in ascending order.
Starter code
public class Main {
public static void main(String[] args) {
int[] nums = {3, 4, 5, 1, 2};
// Minimum is at the rotation point
// Binary search: if mid > right, min is in right half
// Print: Min: 1
}
}Expected output and test cases
- [3,4,5,1,2]
Min: 1
- [4,5,6,7,0,1,2]
Min: 0
- [1] → single element
Min: 1
Hints
- If array not rotated (nums[left] < nums[right]), return nums[left]
- If nums[mid] > nums[right], minimum is in right half
- If nums[mid] <= nums[right], minimum is in left half (including mid)
- When left == right, we found the minimum
Related Data Structures & Algorithms exercises
- Practice Binary Search in Java
- Practice Search in Rotated Sorted Array in Java
- Practice Koko Eating Bananas in Java
Practice all Data Structures & Algorithms exercises · Run this idea in the Java compiler