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.
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 5
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 the cases.
An efficient approach for this problem is to use 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:
- First we find the minimum and maximum elements in the matrix. 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 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 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 implementation of above approach:
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)
This article is contributed by Akshit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Search in a row wise and column wise sorted matrix
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix
- Count zeros in a row wise and column wise sorted matrix
- Largest row-wise and column-wise sorted sub-matrix
- Maximum sum of any submatrix of a Matrix which is sorted row-wise and column-wise
- Kth smallest element in a row-wise and column-wise sorted 2D array | Set 1
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Print all elements in sorted order from row and column wise sorted matrix
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Sort the matrix row-wise and column-wise
- Row-wise vs column-wise traversal of matrix
- Find a common element in all rows of a given row-wise sorted matrix
- Count number of islands where every island is row-wise and column-wise separated
- Leftmost Column with atleast one 1 in a row-wise sorted binary matrix | Set 2
- Leftmost Column with atleast one 1 in a row-wise sorted binary matrix
- Row-wise common elements in two diagonals of a square matrix
- Removing row or column wise duplicates from matrix of characters
- Shift matrix elements row-wise by k
- Program to print elements of a Matrix row-wise skipping alternate elements
- Check if the given matrix is increasing row and column wise