#C13708. Frequent Elements
Frequent Elements
Frequent Elements
You are given a list of integers and an integer n. Your task is to find the n most frequent elements in the list. The output should be sorted primarily by frequency in descending order. In the case of a tie, the element with the larger value comes first. Formally, if we denote the frequency of an element x by f(x), then for two elements x and y the order is defined by:
$$x > y \quad \text{if} \quad f(x) > f(y) \quad \text{or} \quad (f(x) = f(y) \quad \text{and} \quad x > y).$$
If n is greater than the number of distinct elements, output all the distinct elements in the required order. If the input list is empty, output an empty line.
inputFormat
The input is provided via stdin and consists of three lines:
- The first line contains an integer L representing the number of elements in the list.
- The second line contains L space-separated integers representing the list.
- The third line contains an integer n, the number of most frequent elements to output.
outputFormat
Output the n most frequent elements in a single line to stdout, separated by a space. If there are no elements to output (for example, when the list is empty), print an empty line.
## sample4
1 1 2 3
2
1 3