#C13126. Find the Largest K Unique Elements

    ID: 42630 Type: Default 1000ms 256MiB

Find the Largest K Unique Elements

Find the Largest K Unique Elements

Given a list of integers and an optional parameter \(k\) (default is 3), your task is to find the \(k\) largest unique integers in the list and output them in descending order. If the list has fewer than \(k\) unique integers, output all the unique integers sorted in descending order.

For example, if the list is [1, 2, 3, 4, 5] and \(k = 3\), the output should be "5 4 3".

The solution must perform proper input validation:

  • If the first line of input does not represent a list of integers, output All elements in the list should be integers.
  • If the second line (\(k\)) is provided but is not a valid positive integer, output k should be a positive integer.
  • If no input is provided for the list, output Input should be a list.

Your program must read input from standard input (stdin) and produce output to standard output (stdout).

inputFormat

The input is given via stdin. The first line contains the list elements separated by spaces. The second line, which is optional, contains the integer (k). If the second line is omitted or empty, the default value of (k = 3) is used.

outputFormat

Output the (k) largest unique integers in descending order, separated by a space. In case of an error, output the appropriate error message.## sample

1 2 3 4 5
3
5 4 3