Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Median In Row-Wise Sorted Array

Medium Acceptance 69% Points 30

You are given a matrix of size m × n where each row is sorted in ascending order. Your task is to find the median of the matrix. The median is the middle element when all elements of the matrix are arranged in sorted order. If the total number of elements is odd, the median is the middle element.

Examples
Example 1

Example 1

Input: matrix = [[1,3,5],[2,6,9],[3,6,9]]

Output: 5

Explanation: All elements in sorted order are [1,2,3,3,5,6,6,9,9]. The middle element is 5, which is the median.

Example 2

Example 2

Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]

Output: 5

Explanation: All elements in sorted order are [1,2,3,4,5,6,7,8,9]. The middle element is 5, which is the median.

Example 3

Example 3

Input: matrix = [[2,4,6],[3,5,7],[8,9,10]]

Output: 6

Explanation: All elements in sorted order are [2,3,4,5,6,7,8,9,10]. The middle element is 6, which is the median.

Constraints
  • 1 = m, n = 500
  • 1 = matrix[i][j] = 10?
  • Total number of elements (m × n) is always odd
Topics
Binary Search
Companies
Apple Visa
Added 19 Jul 2026 69% acceptance
Ln 1, Col 1Sign in to save your code
1 3 5
2 6 9
3 6 9
5