#K72272. Find the K-th Peak in an Array

    ID: 33717 Type: Default 1000ms 256MiB

Find the K-th Peak in an Array

Find the K-th Peak in an Array

You are given an array of integers. A peak in the array is an element that is strictly greater than its immediate neighbors. In other words, an index \(i\) (with \(1 \le i \le n-2\)) is a peak if \(a_i > a_{i-1}\) and \(a_i > a_{i+1}\). Your task is to find the \(k\)-th peak in the array. If the \(k\)-th peak does not exist, output -1.

Note: For an array of size less than 3, there cannot be any peaks.

Example:

  • For array [1, 3, 2, 4, 5, 2, 1] and \(k = 1\), the peaks are 3 and 5. The 1st peak is 3.
  • For array [1, 2, 3, 4, 3, 2] and \(k = 2\), there is only one peak (4), so the answer is -1.

inputFormat

The input is read from stdin and is formatted as follows:

T
n1 k1
a11 a12 ... a1n1
n2 k2
a21 a22 ... a2n2
...
nT kT
aT1 aT2 ... aTnT

Where:

  • T is the number of test cases.
  • For each test case, the first line contains two integers: n (the number of elements in the array) and k (the peak index to find).
  • The second line contains n space-separated integers representing the array.

outputFormat

For each test case, print the k-th peak in the array on a new line. If the k-th peak does not exist, print -1.

## sample
3
7 1
1 3 2 4 5 2 1
6 2
1 2 3 4 3 2
5 1
5 4 3 2 1
3

-1 -1

</p>