Embercode
✕
Heaps and Priority QueuesCoding problems

Heaps and Priority Queues: coding problems

Write a program that reads the input and prints the answer. Run checks it against every test, including hidden ones.

0 of 8 solved

  • A warehouse keeps the weights of its crates in one long list. The inspector wants the k-th lightest crate, where the lightest is first. Reading all of the weights and sorting them works, but a heap of size k is enough: keep only the k smallest weights seen so far, and the largest of those is the answer.

    **Constraints:** 1 <= k <= n <= 200000. Weights are whole numbers from -1000000000 to 1000000000.

    Input. The first line holds `n` and `k`. The second line holds `n` integers.

    Output. One integer: the k-th smallest weight.

    Example 1 input

    5 2
    7 1 9 3 3
    

    Output

    3
    

    Example 2 input

    4 4
    5 -2 8 0
    

    Output

    8