 All Problems
Kth Smallest Element in Sorted Matrix
medium
binary search
matrix
heap
google
amazon
facebook

Given an n x n matrix where each of the rows and columns is sorted in ascending order, return the kth smallest element in the matrix.

Note that it is the kth smallest element in the sorted order, not the kth distinct element.

You must find a solution with a memory complexity better than O(n²).

Example 1:

Input:
3
1 5 9
10 11 13
12 13 15
k = 8
Output: 13

Example 2:

Input:
1
-5
k = 1
Output: -5

Input format: First line: n. Next n lines: matrix rows. Last line: k.

Output format: The kth smallest element.

Run to check your code against the sample cases, or submit to run every case