#C2572. Find the N Largest Elements
Find the N Largest Elements
Find the N Largest Elements
Given an array of integers and an integer n, your task is to select the n largest elements in the array and print them in descending order. If n is greater than the number of elements in the array, simply output the entire array sorted in descending order.
Note: The result must always be sorted in descending order. For example, if the input array is [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
and n is 3
, then the output should be 9 6 5
.
The solution should read from stdin
and write to stdout
as described in the input and output sections.
inputFormat
The input consists of two lines:
- The first line contains space-separated integers representing the array.
- The second line contains a single integer n indicating how many largest elements to select.
outputFormat
Print the n largest elements of the array in descending order, separated by a single space. If n is greater than the number of elements in the array, print all elements sorted in descending order.## sample
3 1 4 1 5 9 2 6 5 3 5
3
9 6 5