
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- JavaScript Program to Find median in row wise sorted matrix
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix using Python?
- To print all elements in sorted order from row and column wise sorted matrix in Python
- Find a common element in all rows of a given row-wise sorted matrix in C++
- Row-wise vs column-wise traversal of matrix in C++
- How to find the row-wise mode of a matrix in R?
- Python – Sort Matrix by Row Median
- Sort the matrix row-wise and column-wise using Python
- How to search in a row wise and column 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?
- How to search in a row wise increased matrix using C#?
- How to find the row-wise index of non-NA values in a matrix in R?
- Row-wise common elements in two diagonals of a square matrix in C++
- Program to find median of two sorted lists in C++
