medium

Search in Rotated Sorted Array

A sorted array of distinct integers was rotated at an unknown pivot, so it looks like two ascending runs. Given the rotated array and a target, return the index of the target or -1, in logarithmic time.

Constraints
  • 1 ≤ n ≤ 5000
  • -10^4 ≤ nums[i], target ≤ 10^4
  • All values distinct
  • O(log n) required
Examples
in: nums = [4,5,6,7,0,1,2], target = 0
out: 4
in: nums = [4,5,6,7,0,1,2], target = 3
out: -1

Code it yourself

Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?

Choose an approach to check your pattern recognition, or reveal the discussion when you need help.