#K86027. Find the Nth Largest Unique Element

    ID: 36773 Type: Default 1000ms 256MiB

Find the Nth Largest Unique Element

Find the Nth Largest Unique Element

You are given an array of integers and an integer \(n\). Your task is to find the \(n\)th largest unique integer in the array. If the \(n\)th largest unique integer does not exist, output \(-1\).

For example, consider the array [4, 5, 1, 2, 2, 5, 3, 7] and \(n = 3\). The unique elements are \(\{1, 2, 3, 4, 5, 7\}\). When sorted in descending order, they become \(7, 5, 4, 3, 2, 1\). The 3rd largest unique element is 4.

You need to read from standard input (stdin) and output the result on standard output (stdout).

inputFormat

The input consists of two lines:

  • The first line contains two integers \(m\) and \(n\), where \(m\) is the number of elements in the array and \(n\) is the rank of the unique element to find.
  • The second line contains \(m\) space-separated integers representing the array.

For example:

8 3
4 5 1 2 2 5 3 7

outputFormat

Output a single integer, which is the \(n\)th largest unique element in the array. If such an element does not exist, output \(-1\).

For example, the output for the input above should be:

4
## sample
8 3
4 5 1 2 2 5 3 7
4