Kth Smallest Element in a Matrix | GFG POTD | Priority Queue & Binary Search | Java
Автор: The Optimal Approach
Загружено: 2025-12-27
Просмотров: 32
In this video, we solve GeeksforGeeks Problem of the Day – Kth Smallest Element in a Matrix using two efficient approaches in Java.
🔹 Problem Statement
You are given:
A sorted matrix mat[][] (each row and column is sorted)
An integer k
Your task is to find the kth smallest element present in the matrix
🔹 Approaches Covered
🔸 Approach 1: Using Priority Queue (Min Heap)
1. Push the first element into a min-heap
2. Repeatedly extract the minimum k-1 times
3. Insert the next row or column element into the heap
4. The top element after k-1 removals is the answer
📌 Time Complexity:
O( k log n )
📌 Space Complexity:
O( n² )
🔸 Approach 2: Using Binary Search (Optimized)
1. Apply Binary Search on the value range
2. For each mid value, count elements ≤ mid using matrix traversal
3. If count ≥ k → move left, else move right
4. The smallest value satisfying the condition is the answer
📌 Time Complexity:
O( n log (max − min) )
📌 Space Complexity:
constant space O( 1 )
🔹 Resources
Problem Link: https://www.geeksforgeeks.org/problem...
Code Link: https://github.com/sibasundarj8/java-...
👉 This problem is a classic example of combining heap and binary search on answer, and is frequently asked in coding interviews.
👍 Like | 💬 Comment | 🔔 Subscribe for daily GFG POTD solutions in Java
#gfgpotd #geeksforgeekspotd #gfgproblemoftheday
#kthsmallestelement #matrixproblems
#priorityqueue #binarysearch
#javadsa #dsainjava
#interviewpreparation #codinginterview
#competitiveprogramming #dailydsapractice
#java #gfgpractice
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: