VOOZH about

URL: https://www.geeksforgeeks.org/dsa/kth-smallest-element-in-a-row-wise-and-column-wise-sorted-2d-array/

⇱ Kth smallest element in a row-wise and column-wise sorted 2D array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Kth smallest element in a row-wise and column-wise sorted 2D array

Last Updated : 13 Aug, 2025

Given an n Ɨ n matrix mat[][] where each row and column is sorted in non-decreasing order, find the kth smallest element, where k lies in the range [1, n²].

Example:

Input: mat[][] = [[10, 20, 30, 40], k = 3
[15, 25, 35, 45],
[24, 29, 37, 48],
[32, 33, 39, 50]]
Output: 20
Explanation: The sorted order is [10, 15, 20, ...]; the 3rd element is 20.

Input: mat[][] = [[10, 20, 30, 40], k = 7
[15, 25, 35, 45],
[24, 29, 37, 48],
[32, 33, 39, 50]]
Output: 30
Explanation: The sorted order is [10, 15, 20, 24, 25, 29, 30, ...]; the 7th element is 30.

[Naive Approach] Using Sorting - O(n2 Ɨ log n2) Time and O(n) Space

Initialize a 1-dimensional array of size n*n to store all the elements of the mat[][] , we will get our kth minimum element by sorting the 1-dimensional array in non-decreasing order.


Output
20

[Better Approach] Using Priority Queue - O(n2 Ɨ log k) Time and O(k) Space

The idea is to use a max-heap to store and maintain the track of k smallest elements in the heap. If the size of the heap exceeds more than k while inserting the elements , we will pop the top element from max-heap so as to maintain the size of k elements. After successful traversal in mat[][], the top element of the max-heap will be the kth minimum element.


Output
20

[Efficient Approach] Binary Search on Answer

This approach uses binary search to iterate over possible solutions. As answer lies in the range from mat[0][0] to mat[n-1][n-1], So we do a binary search on this range and in each  iteration determine the no of elements smaller than or equal to our current middle element.


Step by Step Implementation:

  • Initialize a variable, say low equals to the mat[0][0] (minimum value of matrix).
  • Initialize a variable, say high equals to the mat[n-1][n-1] (maximum value of matrix).
  • Initialize ans to 0.
  • Perform Binary Search on the range from low to high:
    => Calculate the midpoint in the range say mid.
    => If the countSmallerEqual(function which will return the count of elements less than or equal to mid) is less than k, update low to mid+ 1.
    => if the returned value is greater or equal to k , this can be our possible ans. So, update ans to mid and narrow the search range by setting high to mid - 1.
  • countSmallerEqual (helper function that counts the number of elements in the matrix less than or equal to the given mid.)
    => initialize a pointer, say row and col points to 0 and n-1 respectively. And a variable count = 0.
    => if the mat[row][col] is greater than mid, move left in the matrix by decrementing col.
    => if the mat[row][col] is less than or equal to mid, increment the count as by col + 1 and move down in the matrix by incrementing row.

Output
20

Time Complexity: O(n Ɨ log(max(mat) āˆ’ min(mat))), Binary search is applied over the value range of the matrix, which takes log(max(mat) āˆ’ min(mat)) steps. Each step calls a counting function that runs in O(n) time by scanning from top-right to bottom-left.
Auxiliary Space: O(1), The algorithm uses only fixed variables and no extra data structures, so space usage is constant.

Comment