PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000187. Khoảng cách cặp phần tử nhỏ thứ K (Find K-th Smallest Pair Distance)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Khoảng cách cặp phần tử nhỏ thứ K (Find K-th Smallest Pair Distance)

Find K-th Smallest Pair Distance

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an array of nn nonnegative integers a1,a2,…,ana_1,a_2,\ldots,a_n and a positive integer kk.

For every pair of distinct positions (i,j)(i,j) satisfying 1≤i<j≤n1\le i<j\le n, define the pair distance as ∣ai−aj∣|a_i-a_j|. Pairs with different indices are counted separately even if their distances are equal.

There are n(n−1)/2n(n-1)/2 such pairs. Sort all pair distances in non-decreasing order, preserving repetitions, and find the distance at position kk, numbered from 11.

Input

  • The first line contains integers nn and kk.
  • The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.

Output

  • Print one integer: the kk-th smallest distance among all pairs of distinct positions.

Subtasks

  • Subtask 1 (20 points): 2≤n≤402\le n\le40; 0≤ai≤1060\le a_i\le10^6 for every 1≤i≤n1\le i\le n; 1≤k≤n(n−1)/21\le k\le n(n-1)/2.
  • Subtask 2 (30 points): 2≤n≤7002\le n\le700; 0≤ai≤1060\le a_i\le10^6 for every 1≤i≤n1\le i\le n; 1≤k≤n(n−1)/21\le k\le n(n-1)/2.
  • Subtask 3 (50 points): 2≤n≤1042\le n\le10^4; 0≤ai≤1060\le a_i\le10^6 for every 1≤i≤n1\le i\le n; 1≤k≤n(n−1)/21\le k\le n(n-1)/2.

Examples

Example 1

Input

3 1
1 3 1

Output

0

Explanation

The three pairs are (1,2)(1,2) with distance ∣1−3∣=2|1-3|=2, (1,3)(1,3) with distance ∣1−1∣=0|1-1|=0, and (2,3)(2,3) with distance ∣3−1∣=2|3-1|=2. Sorting gives 0,2,20,2,2, so the first smallest distance is 0.

Example 2

Input

3 3
1 6 1

Output

5

Explanation

The three pairs have distances 5,0,55,0,5. The sorted sequence is 0,5,50,5,5, whose third value is 5.