Dsa
Heap
Medium
Kth Smallest Element in a Sorted Matrix
You are given a K x K integer matrix where each of the rows and columns is sorted in ascending order. Write a program to find the Kth smallest element in it.
Input Format:
- The first line contains an integer K (1 <= K <= 300).
- The next K lines each contain K integers (1 <= matrix[i][j] <= 10^6) representing the rows of the matrix.
- The last line contains an integer K (1 <= K <= K*K).
Output Format:
- Print the Kth smallest element.
Example:
Input:
3
1 5 9
10 11 13
12 13 15
8
Output:
12
Key concepts
heapbinary searchmatrix
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now