Sorted Matrix Problem Statement

You are provided with an N x N matrix MAT consisting of positive integers, where each row and column is sorted in a non-decreasing order.

Your task is to return a list containing all the elements of the matrix in sorted order.

Input:

The first line of input contains a single integer T, representing the number of test cases or queries to be run.
Then the T test cases follow.
The first line of each test case contains a positive integer N, which represents the number of rows and columns in the matrix.
The next 'N' lines, each contains 'N' single space-separated positive integers representing the elements in a row of the matrix.

Output:

For each test case, print a single line containing the elements of the matrix in sorted order.

Example:

Input:
1
4
10 20 30 40
15 20 35 42
27 29 37 46
32 33 38 49

Output:
10 15 20 20 27 29 30 32 33 35 37 38 40 42 46 49

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 100
  • 1 <= MAT[i][j] <= 105
  • Time Limit: 1 sec

Note:

You do not need to print anything. It has already been taken care of. Just implement the given function.
Be the first one to answer
Select
Add answer anonymously...
undefined Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits