Last updated 4 years ago
Suppose a sorted array A is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
0 1 2 4 5 6 7
4 5 6 7 0 1 2
Find the minimum element.
The array will not contain duplicates.
public class Solution { public int findMin(final List<Integer> a) { int start = 0; int end = a.size() - 1; while (start < end) { int mid = (start + end) / 2; if (a.get(start) < a.get(end)) return a.get(start); if (a.get(mid) >= a.get(start)) start = mid + 1; else end = mid; } return a.get(start); } }