22. Median in a Row-wise Sorted Matrix
β GFG solution to find median in a row-wise sorted matrix: efficiently find the median element using binary search on answer technique. π
π§© Problem Description
π Examples
Example 1
Input: mat[][] = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives us [1, 2, 3, 3, 5, 6, 6, 9, 9].
Hence, 5 is median (middle element at position 4 in 0-indexed array).Example 2
Input: mat[][] = [[2, 4, 9],
[3, 6, 7],
[4, 7, 10]]
Output: 6
Explanation: Sorting matrix elements gives us [2, 3, 4, 4, 6, 7, 7, 9, 10].
Hence, 6 is median (middle element at position 4 in 0-indexed array).Example 3
π Constraints
β
My Approach
Binary Search on Answer + Upper Bound
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated