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 size elements.
  • Return -1 if the target is absent.
  • The test cases use distinct values.
Example 1
Input:
nums (int[]) = [4,5,6,7,0,1,2]
size (int) = 7
target (int) = 0
Return:
(int) 4
Example 2
Input:
nums (int[]) = [4,5,6,7,0,1,2]
size (int) = 7
target (int) = 3
Return:
(int) -1
Example 3
Input:
nums (int[]) = [1]
size (int) = 1
target (int) = 1
Return:
(int) 0

Use binary search by identifying which half of the current range is sorted.

Run your code to see the result.