 All Problems
Search in Rotated Sorted Array
medium
arrays
binary search
facebook
amazon
microsoft
google

There is an integer array nums sorted in ascending order (with distinct values), rotated at some pivot. Given the array and a target, return the index of target or -1 if not found. Must run in O(log n).

Example 1:

Input: 4 5 6 7 0 1 2
Target: 0
Output: 4

Example 2:

Input: 4 5 6 7 0 1 2
Target: 3
Output: -1

Constraints:

  • 1 ≤ nums.length ≤ 5000
  • -10⁴ ≤ nums[i], target ≤ 10⁴

Input format: First line: space-separated array. Second line: target.

Output format: Index of target, or -1.

Run to check your code against the sample cases, or submit to run every case