442. Search in Sorted Rotated Array (No Duplicates)

EasyBinary SearchArrayBinary Search

Given a sorted array of distinct integers that has been rotated at some pivot, and a target, return the index of target or -1 if not found. Solve in O(log n).

Input: A rotated sorted array of distinct integers 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 present.
Example 3
Input: [1], 0
Output: -1
Explanation: Array has only 1; 0 not found.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →