Skip to content
BASA DSA

Search In Rotated Sorted Array

MediumBinary Search

A strictly increasing array was rotated, possibly zero times. Return the zero-based index of target, or -1 if absent, in logarithmic time.

Example 1

Input
nums = [6,8,1,3,4]
target = 3
Output
3

Explanation

3 occurs at index 3 after the rotation boundary.

Example 2

Input
nums = [6,8,1,3,4]
target = 5
Output
-1

Explanation

5 is absent despite lying between the smallest and largest values.

Example 3

Input
nums = [2]
target = 2
Output
0

Explanation

The sole element matches at index 0.

Constraints

  • 1 <= len(nums) <= 5000
  • All values are distinct

Function contractsearchArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…