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
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
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 nonnegative integers and a positive integer .
For every pair of distinct positions satisfying , define the pair distance as . Pairs with different indices are counted separately even if their distances are equal.
There are such pairs. Sort all pair distances in non-decreasing order, preserving repetitions, and find the distance at position , numbered from .
Input
- The first line contains integers and .
- The second line contains exactly integers .
Output
- Print one integer: the -th smallest distance among all pairs of distinct positions.
Subtasks
- Subtask 1 (20 points): ; for every ; .
- Subtask 2 (30 points): ; for every ; .
- Subtask 3 (50 points): ; for every ; .
Examples
Example 1
Input
3 1
1 3 1
Output
0
Explanation
The three pairs are with distance , with distance , and with distance . Sorting gives , so the first smallest distance is 0.
Example 2
Input
3 3
1 6 1
Output
5
Explanation
The three pairs have distances . The sorted sequence is , whose third value is 5.