To solve this problem, you'll have to open it on the computer

Kth Smallest Element in a Sorted Matrix

Heaps
medium
Score: 40

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(n2).

Input Format :-

First Parameter - matrix, of size n x n
Second Parameter - k , kth element to be returned.

Output Format :-

Return the kth element in the sorted matrix.

Example 1 :-

Input:
3 3
1 5 9
10 11 13
12 13 15
8
Output:  
13
Explanation: 3 3 represents the size of the matrix. The elements in the matrix are [1,5,9,10,11,12,13,13,15], and the 8th smallest number is 13.

Example 2 :-

Input: 
1 1
-5
1
Output:  
-5

Constraints :-

  • n == matrix.length == matrix[i].length.
  • 1 <= n <= 300.
  • -109 <= matrix[i][j] <= 109.
  • All the rows and columns of matrix are guaranteed to be sorted in non-decreasing order.
  • 1 <= k <= n2.
  • Expected Time Complexity :- O( nlog( matrix[n-1][n-1] - matrix[0][0] )).
  • Expected Space Complexity :- O(1).
Submit code to see the your result here