C++ Program to Find median in row wise sorted matrix Last Updated : 17 Aug, 2023 Summarize Comments Improve Suggest changes Share 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 K kartik Follow Improve Article Tags : C++ Order-Statistics Binary Search statistical-algorithms median-finding +1 More Practice Tags : CPPBinary Search Similar Reads Javascript Program to Find median in row wise sorted matrix 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 9Output : Median is 5If 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 9Output: Median is 4 min read Find median in row wise sorted matrix Given a row-wise sorted matrix mat[][] of order n * m, where the number of rows and columns are always odd. The task is to find the median of the matrix.Note: Median is the middle number in a sorted ascending or descending list of numbers. In case of an even number of elements return the left median 15+ min read Search in row wise sorted matrix Given a row-wise sorted matrix mat[][] and an integer x, the task is to check if x is present in mat[][] or not. Note that there is no ordering among column elements.Examples:Input: x = 9, mat[][] = [[3, 4, 9], [2, 5, 6], [9, 25, 27]]Output: trueExplanation: mat[0][2] is equal to 9.Input: x = 56, ma 6 min read Mean and Median of a matrix Given a sorted matrix of size n*n. Calculate the mean and median of the matrix . Examples: Input : 1 2 3 4 5 6 7 8 9Output :Mean: 5 Median: 5Input : 1 1 1 2 2 2 4 4 4Output :Mean: 2 Median: 2Mean of matrix is = (sum of all elements of matrix)/ (total elements of matrix)Note that this definition does 12 min read Finding Median in a Sorted Linked List Given A sorted linked list of N elements. The task is to find the median in the given Sorted Linked List.We know that median in a sorted array is the middle element. Procedure to find median of N sorted numbers: if N is odd: median is N/2th elementelse median is N/2th element + (N/2+1)th elementExam 14 min read Like