Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Kth Smallest Element in a Sorted Matrix problem solution

YASH PAL, 31 July 2024

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

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]

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

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes