Danny !

3.1

20 votes
Binary Search, Easy, Searching
Problem

Stevie : "Integer sequences, that's my main activity off the pitch. Let's play with them". Great defenders are an asset to any team, just like this guy :

                                                                      enter image description here

You are given a sequence of positive integers in the form of an array A of size N and a number K. Now, you need to perform a certain procedure over this sequence that is as follows :

  1. For each pair (i,j), where 1i<jN store in a list |A[i]A[j]|

  2. Sort this list in non-decreasing order

  3. Print the Kth element of this list (1 indexed)

Note that X denotes the absolute value of any integer X

You need to perform the following procedure and print to output whatever it leads to. Can you do it ?

input Format :

The first line contains 2 space separated integers N and K respectively. The next line contains N space separated integers, where the ith integer denotes A[i].

Output Format :

Print the required answer on a single line.

Constraints :

2N200,000

1A[i]109 , where 1iN

1K(N×(N1))/2

Sample Input
5 4
1 7 9 4 5
Sample Output
3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The List we create after sorting it is :

[1,2,2,3,3,4,4,5,6,8]

As you may observe the 4th element of this 3. So, clearly the answer is 3.

Editor Image

?