Problems > Search a Rotated Array

Search a Rotated Array

Difficulty: 1600 3000 ms 128 MB

binary-search

A sorted array of distinct values was rotated at an unknown pivot. Find the 1-based position of target T, or −1.

Input: the first line has N and T; the second line has the rotated array.

Output: the position of T, or −1.

Constraints:

  • 1 ≤ N ≤ 105
  • values distinct, |values| ≤ 106

Sample Tests

Input 1
7 0
4 5 6 7 0 1 2
Output 1
5
Input 2
3 3
3 1 2
Output 2
1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.