```
public int search(int[] nums, int target) {
int i = 0, n = nums.length;
while (i + 1 < n && nums[i] <= nums[i + 1])
i++;
int left = Arrays.binarySearch(nums, 0, i + 1, target);
int right = Arrays.binarySearch(nums, i + 1, n, target);
return left < 0 && right < 0 ? -1 : Math.max(left, right);
}
```