#C1618. Kth Smallest Element in a Sorted Matrix
Kth Smallest Element in a Sorted Matrix
Kth Smallest Element in a Sorted Matrix
You are given an n x n matrix where each of the rows and columns is sorted in ascending order. Your task is to find the kth smallest element in the matrix.
Note: The matrix is guaranteed to have its rows and columns sorted in non-decreasing order. Efficient solutions may use a min-heap to achieve the optimal performance.
inputFormat
The input is given via standard input (stdin).
The first line contains two integers n
and k
, where n
is the dimension of the matrix and k
is the order of the smallest element to find (1-indexed).
This is followed by n
lines, each containing n
space-separated integers, representing the rows of the sorted matrix.
outputFormat
Output a single integer to standard output (stdout) — the kth smallest element in the matrix.## sample
3 8
1 5 9
10 11 13
12 13 15
13