Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Kth Smallest Element in a Sorted Matrix problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Kth Smallest Element in a Sorted Matrix problem solution, we have given an n x n matrix where each of the rows and columns are 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.

Leetcode Kth Smallest Element in a Sorted Matrix problem solution

Leetcode Kth Smallest Element in a Sorted Matrix problem solution in Python.

class Solution:
    def kthSmallest(self, matrix: List[List[int]], k: int) -> int:
        if not matrix:
            return 0
        for i in matrix[1:]:
            matrix[0] += i
        return sorted(matrix[0])[k - 1]

Kth Smallest Element in a Sorted Matrix problem solution in Java.

class Solution {
    public int kthSmallest(int[][] matrix, int k) {
        PriorityQueue<Integer> queue = new PriorityQueue<Integer>(Collections.reverseOrder());
        
        for(int i=0; i<matrix.length; i++) {
            for(int j = 0; j<matrix[i].length; j++) {
                if(queue.size() == k && queue.peek() > matrix[i][j]) queue.poll();
                if(queue.size() < k) queue.offer(matrix[i][j]);
            }
        }
        return queue.peek();
                                                  
    }
}

Problem solution in C++.

class Solution {
public:
    int kthSmallest(vector<vector<int>>& matrix, int k) {
        priority_queue<int,vector<int>,  
                       greater<int>>pq;
        for(auto i : matrix){
            for(auto x: i){
                pq.push(x);
            }
        }
        k = k-1;
        while(k--){
            pq.pop();
        }
        return pq.top();
    }
};

Problem solution in C.

int kthSmallest(int** matrix, int n, int useless, int k) {
    int small=matrix[0][0], big=matrix[n-1][n-1], mid, cnt, i, j;
    while(small<big) {
        mid=small+big>>1;
        cnt=0;
        j=n-1;
        for(i=0;i<n;i++) {
            while(j>=0&&matrix[i][j]>mid) --j;
            cnt+=j+1;
        }
        if(cnt<k) small=mid+1;
        else big=mid;
    }
    return big;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes