Campsites Along the Trail

medium binary-search greedy sorting arrays 1000 ms · 256 MB

Campsites Along the Trail

A long-distance trail has been surveyed, and n possible campsite markers have been staked into the ground at distinct integer mile positions along the trail. The trail is a straight line, so a marker is fully described by its mile position.

A hiking club is bringing k groups on a trip. Each group needs its own marker to camp at, and no marker may host more than one group. The groups want to spread out as much as possible: among all ways of choosing k of the n markers, they want the one that makes the smallest distance between any two chosen markers as large as possible.

Given the positions of the markers, find this largest possible value of "the smallest distance between any two chosen markers".

Input

The first line contains two integers n and k (2 ≤ k ≤ n ≤ 2·10^5) — the number of surveyed markers and the number of groups.

The second line contains n distinct integers p_1, p_2, ..., p_n (1 ≤ p_i ≤ 10^9) — the mile positions of the markers, in no particular order.

Output

Print a single integer: the maximum, over all ways to choose k of the n markers, of the minimum distance between any two chosen markers.

Notes

In the first example, the markers (sorted) sit at miles 1, 2, 4, 8, 9. Choosing miles 1, 4, and 8 gives pairwise gaps of 3 and 4 (and 7 between the outermost pair), so the smallest gap among the chosen sites is 3 — no choice of 3 markers does better.

In the second example k equals n, so every marker must be used; the answer is simply the smallest gap between consecutive markers once they are sorted (miles 1, 5, 10 give gaps 4 and 5, so the answer is 4).

Example 1

Input

5 3
4 1 9 2 8

Output

3

Example 2

Input

3 3
10 1 5

Output

4

Hints · reveal one at a time if you're stuck

Hint 1

Finding the best spacing directly is awkward, but checking whether a given minimum distance d can be achieved may be easy. Once the markers are sorted, what happens if you simply walk left to right and take each marker that is far enough from the last one you kept?

Hint 2

The achievable distances form a range that is closed under shrinking, so you can binary search on the answer. Each check is a single sweep over the sorted positions.

Hint 3

In the check, keeping the leftmost marker first and then taking every marker at least d beyond the last one kept is optimal, so you only need to count how many you get. Make sure a gap of exactly d is accepted.

report an issue Prefer your own editor? sooks prep 73 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.