Write a method named binarySearchRotated
that performs a binary search for a value in a "rotated" sorted array.
Your method accepts two parameters: the array and the value to search for, and returns the index where the element is found in the array, or -1
if not found.
The array's elements are in sorted order except "rotated" right by a given amount.
For example, if an array a
stores [-8, 0, 3, 6, 10, 19, 22]
but is rotated right by 3, its elements would be [10, 19, 22, -8, 0, 3, 6]
.
So the call of binarySearchRotated(a, 22)
would return 2
.
You may assume that the array is in sorted/rotated order and contains no duplicates.
Your method must be O(log N) where N is the length of the array.