Menu Close

Find the kth smallest element in the given 2D array.

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 =
        11, 22, 33, 44
        55, 65, 75, 85
        94, 99, 97, 98
        12, 13, 19, 10 
Output: 13
Explanation: The 3rd smallest element is 13
import random
k=5
L=[[11,22,33,44],
	[55,65,75,85],
	[94,99,97,98],
	[12,13,19,10]]
Q=[]
for i in L:
	Q=Q+i
Q=sorted(Q)

print(Q[k])

Input :  k = 4 and array =
11, 22, 33, 44
55, 65, 75, 85
94, 99, 97, 98
12, 13, 19, 10

Output:
19


Input :  k = 7 and array =
11, 22, 33, 44
55, 65, 75, 85
94, 99, 97, 98
12, 13, 19, 10
Output:
44


Illustration of Output :

Executed using python3 in terminal Linux

More Q