#K36607. Find the K Largest Elements in a Matrix

    ID: 25792 Type: Default 1000ms 256MiB

Find the K Largest Elements in a Matrix

Find the K Largest Elements in a Matrix

You are given an n×n matrix of integers and a number k. Your task is to find the k largest elements in the matrix and output them in descending order.

Input Format: The first line contains an integer n representing the size of the matrix. The next n lines each contain n space-separated integers representing the matrix. The last line contains an integer k representing the number of largest elements to be found.

Note: The matrix will always be square. It is guaranteed that k is between 1 and n×n.

Example:

Input:
4
10 20 30 40
15 25 35 45
27 29 37 48
32 33 39 50
3

Output: 50 48 45

</p>

inputFormat

Input:

  • The first line contains the integer \( n \), where \( n \) is the dimension of the square matrix.
  • The following \( n \) lines each contain \( n \) integers separated by spaces representing the elements of the matrix.
  • The last line contains the integer \( k \), the number of largest elements to output.

outputFormat

Output:

  • Print the \( k \) largest elements in the matrix in descending order on a single line, separated by spaces.
## sample
4
10 20 30 40
15 25 35 45
27 29 37 48
32 33 39 50
3
50 48 45