All problems

Problem 884

Search in a Mountain Array

Hard · 30 pts · Array, Binary Search

Problem

A mountain array strictly increases up to a peak and then strictly decreases, and it has at least 3 elements. Find the smallest index (0-based) where TARGET appears, or print -1. A value can appear at most twice, once on each side of the peak. Find the peak with a binary search on the slope, then binary search the increasing part first and, only if the target is not there, the decreasing part.

Input

The first line contains N and TARGET. The second line contains the N values of the mountain array.

Output

Print the smallest index of TARGET, or -1.

Constraints

3 <= N <= 10000; 0 <= values, TARGET <= 10^9; the array is a mountain

Example input

7 3
1 2 3 4 5 3 1

Example output

2

Explanation

In 1 2 3 4 5 3 1 the value 3 appears at indices 2 and 5. The smaller index is 2, so the answer is 2.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Kth Smallest Prime Fraction Next: Minimum Time to Repair Cars