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.Your task is to complete the function **kthSmallest** which takes two arguments the first is a matrix (mat) and sec argument is the order of the matrix (n) and the function returns the kth smallest element in the matrix.

*For example, consider the following 2D array.
*

15, 25, 35, 45

24, 29, 37, 48

32, 33, 39, 50

The 3rd smallest element is 20 and 7th smallest element is 30

The first line of input is an integer T denoting the no of test cases . Then T lines follow . The first line of each test case contains an integer N denoting the size of the matrix then in the next line are N*N space separated values of the matrix . The third line contains an integer k.

For each test case print in a new line the kth smallest element of the matrix .

1<=T<=100

1<= N<=20

1<= M[][]<=100

1

4

16 28 60 64 22 41 63 91 27 50 87 93 36 78 87 94

3

27

Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

the_coder95 | 465 |

thanuvinu94 | 388 |

adityasuman2025 | 348 |

Senbonzakura_Kageyoshi | 321 |

PrakharBansal1 | 294 |

the_coder95 | 1691 |

RishabhTanwar1 | 1110 |

thanuvinu94 | 1064 |

samay_kam_hai | 726 |

adityasuman2025 | 646 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4897 |

Quandray | 4547 |

Login to report an issue on this page.