Intermediate Level
Implement the method searchRotatedArray that finds a target in a rotated sorted array.
A rotated sorted array is sorted but shifted at one pivot. Return the zero-based index of target.
The task is designed to test careful handling of edge cases, not only the most common input.
- Use only the first
sizeelements. - Return
-1if the target is absent. - The test cases use distinct values.
Example 1
Input
nums (int[]) =
[4,5,6,7,0,1,2]size (int) =
7target (int) =
0Returns (int)
4Example 2
Input
nums (int[]) =
[4,5,6,7,0,1,2]size (int) =
7target (int) =
3Returns (int)
-1Example 3
Input
nums (int[]) =
[1]size (int) =
1target (int) =
1Returns (int)
0Use binary search by identifying which half of the current range is sorted.
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run