Asked inFidelity International,Full Stack Engineering Intern

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
Add answer anonymously...
Fidelity International Full Stack Engineering Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter