← Practice · Binary search

Search in Rotated Sorted Array Medium

A sorted array of distinct integers was rotated at an unknown pivot (e.g. [0,1,2,4,5,6,7] became [4,5,6,7,0,1,2]). Given the rotated array and a target, return the target's index or -1. Must run in O(log n).

Examples

  • nums = [4,5,6,7,0,1,2], target = 0 -> 4
  • nums = [4,5,6,7,0,1,2], target = 3 -> -1

Define a function named search in JavaScript.

00:00▶ Visualize

JavaScript · Tab indents · Ctrl/⌘+Enter runs the tests