- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find median in row wise sorted matrix in C++

In this problem, we are given a 2D array mat[r][c] whose elements are sorted row-wise. Our task is to Find median in a row-wise sorted matrix.

**Description** − we need to find the median of elements of the matrix.

**Let’s take an example to understand the problem,**

## Input

mat = { {2, 4, 7}, {5, 6, 8}, {4, 8, 9} }

## Output

6

## Explanation

The elements of the matrix stored in array are &minus

{2, 4, 4, 5, 6, 7, 8, 8, 9} The median is 6.

## Solution Approach

A simple solution to the problem is by storing all elements of the array. Then finding the median element by sorting the array.

A more effective solution to the problem is finding the median element using the fact the is has exactly (r*c)/2 smaller element in the matrix. And we will find the element in the array that follows this condition. For this we will use the binary search on the matrix taking the smallest and largest element of the matrix and then we will find the middle of the range and check the number of smaller elements in it. If it equal to r*c/2 then return the number. If it is greater than (r*c)/2, then we will change the largest element to the element smaller than the middle found and do the same for smallest if the count is smaller than (r*c)/2.

The count of elements smaller than the middle element, we can either count all elements row-wise by finding the index of the first element greater than middle or simply use the upper_bound which is an inbuilt function in c++.

**Program to illustrate the working of our solution,**

## Example

#include<bits/stdc++.h> using namespace std; #define c 3 #define r 3 int findMedian(int mat[][c]) { int smallest = INT_MAX, largest = INT_MIN; for (int i=0; i<r; i++) { if (mat[i][0] < smallest) smallest = mat[i][0]; if (mat[i][c-1] > largest) largest = mat[i][c-1]; } while (smallest < largest){ int mid = smallest + (largest - smallest) / 2; int smallCount = 0; for (int i = 0; i < r; ++i) smallCount += upper_bound(mat[i], mat[i]+c, mid) - mat[i]; if (smallCount < ( (r * c + 1) / 2 )) smallest = mid + 1; else largest = mid; } return smallest; } int main(){ int mat[][c]= { {2, 5, 7}, {4, 6, 8}, {1, 8, 9} }; cout<<"The median of the matrix is "<<findMedian(mat); return 0; }

## Output

The median of the matrix is 6

- Related Questions & Answers
- Find a common element in all rows of a given row-wise sorted matrix in C++
- . Find a common element in all rows of a given row-wise sorted matrix
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix using Python?
- Row-wise vs column-wise traversal of matrix in C++
- To print all elements in sorted order from row and column wise sorted matrix in Python
- Python – Sort Matrix by Row Median
- Program to find median of two sorted lists in C++
- How to search in a row wise and column wise increased matrix using C#?
- How to find the row-wise mode of a matrix in R?
- Median of Two Sorted Arrays in C++
- How to search in a row wise increased matrix using C#?
- How to divide matrix rows in R by row median?
- How to find the row median of columns having same name in R matrix?
- Row-wise common elements in two diagonals of a square matrix in C++
- Finding Median in a Sorted Linked List in C++