Python heapq to find K'th smallest element in a 2D array
Last Updated :
23 Apr, 2023
Given an n x n matrix and integer k. Find the k'th smallest element in the given 2D array. Examples:
Input : mat = [[10, 25, 20, 40],
[15, 45, 35, 30],
[24, 29, 37, 48],
[32, 33, 39, 50]]
k = 7
Output : 7th smallest element is 30
We will use similar approach like K’th Smallest/Largest Element in Unsorted Array to solve this problem.
- Create an empty min heap using heapq in python.
- Now assign first row (list) in result variable and convert result list into min heap using heapify method.
- Now traverse remaining row elements and push them into created min heap.
- Now get k'th smallest element using nsmallest(k, iterable) method of heapq module.
Implementation:
Python3
# Function to find K'th smallest element in
# a 2D array in Python
import heapq
def kthSmallest(input):
# assign first row to result variable
# and convert it into min heap
result = input[0]
heapq.heapify(result)
# now traverse remaining rows and push
# elements in min heap
for row in input[1:]:
for ele in row:
heapq.heappush(result,ele)
# get list of first k smallest element because
# nsmallest(k,list) method returns first k
# smallest element now print last element of
# that list
kSmallest = heapq.nsmallest(k,result)
print (k,"th smallest element is ",kSmallest[-1])
# Driver program
if __name__ == "__main__":
input = [[10, 25, 20, 40],
[15, 45, 35, 30],
[24, 29, 37, 48],
[32, 33, 39, 50]]
k = 7
kthSmallest(input)
Output7 th smallest element is 30
Approach#2: Using a Max Heap
We can create a max heap and keep adding the first row of elements in it. As we need to find the kth smallest element, we pop k-1 elements from the heap and add the next element from the same column as the popped element. The kth smallest element will be at the top of the heap after this process.
Algorithm
1. Create an empty max heap.
2. Push first row of elements in the heap.
3. Pop k-1 smallest elements from the heap and add next element from the same column as the popped element.
4. Return the kth smallest element.
Python3
import heapq
def kthSmallest(mat, k):
# create a max heap with first row of elements
heap = []
for i in range(len(mat[0])):
heapq.heappush(heap, (-mat[0][i], 0, i))
# pop k-1 smallest elements from the heap
for i in range(k-1):
num, row, col = heapq.heappop(heap)
# add next element from the same row
if row < len(mat)-1:
heapq.heappush(heap, (-mat[row+1][col], row+1, col))
# the kth smallest element is now at the top of the heap
return -heapq.heappop(heap)[0]+1
mat = [[10, 25, 20, 40],
[15, 45, 35, 30],
[24, 29, 37, 48],
[32, 33, 39, 50]]
k = 7
print("Kth smallest element is:", kthSmallest(mat, k))
OutputKth smallest element is: 30
Time complexity: O(klog(n)), where n is the number of rows in the matrix. We need to pop k-1 elements and add at most n-1 elements for each popped element in the worst case. So, the time complexity is proportional to klog(n).
Space complexity: O(n), where n is the number of columns in the matrix. We are keeping a max heap with elements from the first row of the matrix. The maximum number of elements we need to store is equal to the number of columns in the matrix.
Similar Reads
Python | Find smallest element greater than K This problem involves searching through a list to identify the smallest number that is still larger than K. We will explore different methods to achieve this in Python In this article, we'll look at simple ways to find the smallest element greater than k in a list using Python.Using Binary SearchBin
3 min read
Kâth Smallest Element in Unsorted Array Given an array arr[] of N distinct elements and a number K, where K is smaller than the size of the array. Find the K'th smallest element in the given array. Examples:Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 3 Output: 7Input: arr[] = {7, 10, 4, 3, 20, 15}, K = 4 Output: 10 Table of Content[Naive Ap
15 min read
Finding the k smallest values of a NumPy array In this article, let us see how to find the k number of the smallest values from a NumPy array. Examples: Input: [1,3,5,2,4,6] k = 3 Output: [1,2,3] Method 1: Using np.sort() . Approach: Create a NumPy array.Determine the value of k.Sort the array in ascending order using the sort() method.Print th
2 min read
Kâth Smallest/Largest Element in Unsorted Array | Expected Linear Time Given an array of distinct integers and an integer k, where k is smaller than the array's size, the task is to find the k'th smallest element in the array.Examples:Input: arr = [7, 10, 4, 3, 20, 15], k = 3Output: 7Explanation: The sorted array is [3, 4, 7, 10, 15, 20], so the 3rd smallest element is
10 min read
Kth smallest element in a row-wise and column-wise sorted 2D array Given an n x n matrix, every row and column is sorted in non-decreasing order. Given a number K where K lies in the range [1, n*n], find the Kth smallest element in the given 2D matrix.Example:Input: mat =[[10, 20, 30, 40], [15, 25, 35, 45], [24, 29, 37, 48], [32, 33, 39, 50]]K = 3Output: 20Explanat
15+ min read