Given an n x n matrix, where every row and column is sorted in non-decreasing order. Find the kth smallest element in the given 2D array.
Input:k = 3 and array = 10, 20, 30, 40 15, 25, 35, 45 24, 29, 37, 48 32, 33, 39, 50 Output: 20 Explanation: The 3rd smallest element is 20 Input:k = 7 and array = 10, 20, 30, 40 15, 25, 35, 45 24, 29, 37, 48 32, 33, 39, 50 Output: 30 Explanation: The 7th smallest element is 30
Approach: So the idea is to find the kth minimum element. Each row and each column is sorted. So it can be thought as C sorted lists and the lists have to be merged into a single list, the kth element of the list has to be found out. So the approach is similar, the only difference is when the kth element is found the loop ends.
- The idea is to use min heap. Create a Min-Heap to store the elements
- Traverse the first row from start to end and build a min heap of elements from first row. A heap entry also stores row number and column number.
- Now Run a loop k times to extract min element from heap in each iteration
- Get minimum element (or root) from Min-Heap.
- Find row number and column number of the minimum element.
- Replace root with the next element from same column and min-heapify the root.
- Print the last extracted element, which is the kth minimum element
7th smallest element is 30
- Time Complexity: The above solution involves following steps.
- Building a min-heap which takes O(n) time
- Heapify k times which takes O(k Logn) time.
- Space Complexity: O(R), where R is the length of a row, as the Min-Heap stores one row at a time.
The above code can be optimized to build a heap of size k when k is smaller than n. In that case, the kth smallest element must be in first k rows and k columns.
We will soon be publishing more efficient algorithms for finding the kth smallest element.
This article is compiled by Ravi Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sort Matrix in alternating ascending and descending order rowwise
- Kth smallest or largest element in unsorted Array | Set 4
- Print X array elements closest to the Kth smallest element in the array
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Kth smallest element from an array of intervals
- Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array
- Swap Kth node from beginning with Kth node from end in a Linked List
- Kth Smallest Element of a Matrix of given dimensions filled with product of indices
- Kth Smallest element in a Perfect Binary Search Tree
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- kth smallest/largest in a small range unsorted array
- Given an array and two integers l and r, find the kth largest element in the range [l, r]
- Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array
- Smallest submatrix with Kth maximum XOR
- Find maximum sum taking every Kth element in the array
- Sort a nearly sorted (or K sorted) array
- Find the smallest and second smallest elements in an array
- Print all elements in sorted order from row and column wise sorted matrix
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Length of smallest subarray to be removed such that the remaining array is sorted