MentorNode
Start free
Binary Search & Search Space/medium

Search in Rotated Sorted Array

binary-searchsearch-in-rotated-sorted-array

Search in Rotated Sorted Array

There is an integer array nums sorted in ascending order (with distinct values) that was rotated at an unknown pivot index.

Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.

Example 1

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Constraints

  • 1 <= nums.length <= 5000
  • -10^4 <= nums[i] <= 10^4
  • All values of nums are unique.

Loading workspace editor...
Ready to run
Input:
{
  "nums": [
    4,
    5,
    6,
    7,
    0,
    1,
    2
  ],
  "target": 0
}
Expected Output:
4