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
Returns (int) 4

Example 2

Input
nums (int[]) = [4,5,6,7,0,1,2]
size (int) = 7
target (int) = 3
Returns (int) -1

Example 3

Input
nums (int[]) = [1]
size (int) = 1
target (int) = 1
Returns (int) 0

Use 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

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.