Zero To DSAZero To DSA
Privacy Policy·Support on Ko‑fi
Search in Rotated Sorted Array

Binary Search

easy
Time: O(log n)
Space: O(1)

Given a sorted array of integers and a target, return the index of the target. If not found, return -1.

Constraints

  • 1 <= nums.length <= 10^4
  • nums is sorted in ascending order.

Examples

Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1