Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Median In Row-Wise Sorted Array

Medium Acceptance 69.00% Points 30.00

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.

Hints
Hint 1
N/A
Constraints
  • 1 = m, n = 500
  • 1 = matrix[i][j] = 10?
  • Total number of elements (m × n) is always odd
Companies
Apple Visa
Topics
Binary Search
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions