You are given a sequence of integers of length n and integer number k. You should print any integer number x in the range of [1;109] (i.e. 1≤x≤109) such that exactly k elements of given sequence are less than or equal to x
.
Note that the sequence can contain equal elements.
If there is no such x
, print "-1" (without quotes).
Input
The first line of the input contains integer numbers n
and k (1≤n≤2⋅105, 0≤k≤n). The second line of the input contains n integer numbers a1,a2,…,an (1≤ai≤109
) — the sequence itself.
Output
Print any integer number x
from range [1;109] such that exactly k elements of given sequence is less or equal to x
.
If there is no such x
, print "-1" (without quotes).
Examples
Input
Copy
7 4 3 7 5 1 10 3 20
Output
Copy
6
Input
Copy
7 2 3 7 5 1 10 3 20
Output
Copy
-1
这个我主要卡在k=0时的情况了,我想错了;
其实k=0的时候不是只会输出-1;
例如数组里面都大于等于2,那么若k=0,则x可以是1;
若都大于等于1,min=1,则输出-1!!!!!!!!!!!
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
int arr[N];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=0; i<n; i++)
scanf("%d",&arr[i]);
sort(arr,arr+n);
if(k==0)
{
int ans=arr[0]-1;//k=0时候有解,并不是都输出-1;
if(ans<1) printf("-1\n");
else printf("%d\n",ans);
}
else if(arr[k-1]==arr[k]) printf("-1\n");
else printf("%d\n",arr[k-1]);
return 0;
}