77. Search in Rotated Sorted Array

MediumArrayArray

An integer array was sorted in ascending order then rotated at an unknown pivot. Given nums and target, return the index of target or -1. Must run in O(log n) time.

Input: A rotated sorted array nums (distinct values) and integer target.

Output: Index of target or -1.

Examples

Example 1
Input: [4,5,6,7,0,1,2],0
Output: 4
Explanation: 0 is at index 4.
Example 2
Input: [4,5,6,7,0,1,2],3
Output: -1
Explanation: 3 not found.
Example 3
Input: [1],0
Output: -1
Explanation: 0 not in [1].

Constraints

Asked by

AmazonBloombergPaytmMicrosoftOracleFlipkart
Solve this problem in the editor →