C++ Program to Find median in row wise sorted matrix Last Updated : 17 Aug, 2023 Comments Improve Suggest changes Like Article Like Report We are given a row-wise sorted matrix of size r*c, we need to find the median of the matrix given. It is assumed that r*c is always odd.Examples: Input : 1 3 5 2 6 9 3 6 9 Output : Median is 5 If we put all the values in a sorted array A[] = 1 2 3 3 5 6 6 9 9) Input: 1 3 4 2 5 6 7 8 9 Output: Median is 5Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. Simple Method: The simplest method to solve this problem is to store all the elements of the given matrix in an array of size r*c. Then we can either sort the array and find the median element in O(r*clog(r*c)) or we can use the approach discussed here to find the median in O(r*c). Auxiliary space required will be O(r*c) in both cases.An efficient approach for this problem is to use a binary search algorithm. The idea is that for a number to be median there should be exactly (n/2) numbers which are less than this number. So, we try to find the count of numbers less than all the numbers. Below is the step by step algorithm for this approach: Algorithm: First, we find the minimum and maximum elements in the matrix. The minimum element can be easily found by comparing the first element of each row, and similarly, the maximum element can be found by comparing the last element of each row.Then we use binary search on our range of numbers from minimum to maximum, we find the mid of the min and max and get a count of numbers less than our mid. And accordingly change the min or max.For a number to be median, there should be (r*c)/2 numbers smaller than that number. So for every number, we get the count of numbers less than that by using upper_bound() in each row of the matrix, if it is less than the required count, the median must be greater than the selected number, else the median must be less than or equal to the selected number. Below is the implementation of the above approach: C++ // C++ program to find median of a matrix // sorted row wise #include<bits/stdc++.h> using namespace std; const int MAX = 100; // function to find median in the matrix int binaryMedian(int m[][MAX], int r ,int c) { int min = INT_MAX, max = INT_MIN; for (int i=0; i<r; i++) { // Finding the minimum element if (m[i][0] < min) min = m[i][0]; // Finding the maximum element if (m[i][c-1] > max) max = m[i][c-1]; } int desired = (r * c + 1) / 2; while (min < max) { int mid = min + (max - min) / 2; int place = 0; // Find count of elements smaller than mid for (int i = 0; i < r; ++i) place += upper_bound(m[i], m[i]+c, mid) - m[i]; if (place < desired) min = mid + 1; else max = mid; } return min; } // driver program to check above functions int main() { int r = 3, c = 3; int m[][MAX]= { {1,3,5}, {2,6,9}, {3,6,9} }; cout << "Median is " << binaryMedian(m, r, c) << endl; return 0; } Output: Median is 5 Time Complexity: O(32 * r * log(c)). The upper bound function will take log(c) time and is performed for each row. And since the numbers will be max of 32 bit, so binary search of numbers from min to max will be performed in at most 32 ( log2(2^32) = 32 ) operations. Auxiliary Space : O(1) Please refer complete article on Find median in row wise sorted matrix for more details! Comment More infoAdvertise with us Next Article C++ Program to Find median in row wise sorted matrix kartik Follow Improve Article Tags : Matrix C++ Programs C++ DSA Order-Statistics Binary Search statistical-algorithms median-finding +4 More Practice Tags : CPPBinary SearchMatrix Similar Reads C++ Program to Sort the matrix row-wise and column-wise Given a n x n matrix. The problem is to sort the matrix row-wise and column wise.Examples:Â Â Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} } Output : 1 3 4 2 5 7 6 8 9 Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 3 min read C++ Program to Sort the given matrix Given a n x n matrix. The problem is to sort the given matrix in strict order. Here strict order means that matrix is sorted in a way such that all elements in a row are sorted in increasing order and for row âiâ, where 1 Examples:Â Â Input : mat[][] = { {5, 4, 7}, {1, 3, 8}, {2, 9, 6} } Output : 1 2 2 min read How to Find the Median of Sorted Vector in C++? In C++, vectors are dynamic arrays and the median is a middle value when data is sorted in ascending order. In this article, we will learn how to find the median of all elements in a sorted vector in C++. Example Input: myVector = {10, 20, 30, 40, 50}; Output: Median is 30Finding Median of Sorted Ve 2 min read C++ Program to Print matrix in zag-zag fashion Given a matrix of 2D array of n rows and m columns. Print this matrix in ZIG-ZAG fashion as shown in figure. Example: Input: 1 2 3 4 5 6 7 8 9 Output: 1 2 4 7 5 3 6 8 9 Approach of C++ code The approach is simple. Just simply iterate over every diagonal elements one at a time and change the directio 3 min read C++ Program For Row Wise Sorting in 2D Array Given a 2D array, sort each row of this array and print the result.Examples: Input: 77 11 22 3 11 89 1 12 32 11 56 7 11 22 44 33 Output: 3 11 22 77 1 11 12 89 7 11 32 56 11 22 33 44 Input: 8 6 4 5 3 5 2 1 9 7 4 2 7 8 9 5 Output: 4 5 6 8 1 2 3 5 2 4 7 9 5 7 8 9 Method 1 (Using Bubble Sort): Start ite 3 min read C++ program to Convert a Matrix to Sparse Matrix Given a matrix with most of its elements as 0, convert this matrix to sparse matrix in C++Examples: Input: Matrix: 0 1 1 1 2 2 2 1 3 3 2 5 4 3 4 Output: Sparse Matrix: 0 1 0 0 0 0 2 0 0 3 0 0 0 0 5 0 0 0 0 4 Explanation: Here the Sparse matrix is represented in the form Row Column Value Hence the ro 3 min read How to Find the Median of a Sorted Array in C++? In C++, the median of a sorted array is the middle element if the array size is odd, or the average of the two middle elements if the array size is even. In this article, we will learn how to find the median of a sorted array in C++. Example Input: myArray: {1, 2, 3, 4, 5} Output: Median of the Arra 2 min read Finding Median of unsorted Array in linear time using C++ STL Given an unsorted array arr[] having N elements, the task is to find out the median of the array in linear time complexity. Examples: Input: N = 5, arr[] = {4, 1, 2, 6, 5} Output: 4 Explanation: Since N = 5, which is odd, therefore the median is the 3rd element in the sorted array. The 3rd element i 3 min read How to Find the Median of Array in C++? In C++, the array is a collection of elements of the same type, In this article, we will learn how to find the median of the array in C++. The median of the array will be the middle element if the number of elements is odd or the average of two middle elements if the number of elements is even in th 2 min read Find Median of two given Vectors Given two vectors, a and b of different sizes, where array a has m number of elements and array b has n number of elements. The task is to find the median of two vectors. This problem is an extension of the Median of two sorted arrays of different sizes problem. Example: Input: a = {1, 4} b = {2} Ou 7 min read Like