Matrix Median Problem Statement

You are provided with a matrix containing 'N' rows and 'M' columns filled with integers. Each row is sorted in non-decreasing order. Your task is to find the overall median of the matrix. This means if all elements of the matrix are listed in a single line, you need to determine the median of that linear array.

Input:

The first line contains a single integer 'T' representing the number of test cases.

For each test case, the first line contains two integers 'N' and 'M', denoting the number of rows and columns, respectively.

The next 'N' lines contain 'M' space-separated integers each, representing the elements in the matrix.

Output:

For each test case, print an integer which is the overall median of the given matrix.

Each test case's output should be on a separate line.

Example:

Input:
2
3 3
1 3 5
2 6 9
3 6 9
2 3
1 2 3
4 5 6
Output:
5
3

Explanation:

For the first test case, the merged sorted list of all matrix elements is [1, 2, 3, 3, 5, 6, 6, 9, 9], and the median is 5.

Constraints:

  • 1 <= 'T' <= 50
  • 1 <= 'N', 'M' <= 100
  • 1 <= MATRIX['I']['J'] <= 105
  • N * M is always an odd number.

Note:

Implementation detail: You only need to implement the required function; no need to handle input/output operations.
AnswerBot
4mo

Find the overall median of a matrix with sorted rows.

  • Merge all elements of the matrix into a single sorted array

  • Calculate the median of the merged array

  • Handle odd number of elements by directly findin...read more

Help your peers!
Select
Add answer anonymously...

Amazon Software Developer Intern interview questions & answers

A Software Developer Intern was asked 3mo agoQ. What is the code to calculate the distance between two nodes in a binary tree?
A Software Developer Intern was asked 4mo agoQ. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
A Software Developer Intern was asked 4mo agoQ. For a given array, how would you count the number of inversions?

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 3mo agoQ1. What is the code to calculate the distance between two nodes in a binary tree?
A Software Developer Intern was asked 4mo agoQ2. For a given array, how would you count the number of inversions?
A Software Developer Intern was asked 4mo agoQ3. Given a tree, find its diameter (the longest path between two nodes in the tree)...read more
Amazon Software Developer Intern 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