Given a sorted matrix of size n*n. Calculate the mean and median of the matrix .
Input : 1 2 3 4 5 6 7 8 9 Output :Mean: 5 Median: 5 Input : 1 1 1 2 2 2 4 4 4 Output :Mean: 2 Median: 2
Mean of matrix is = (sum of all elements of matrix)/ (total elements of matrix) Note that this definition doesn't require matrix to be sorted and works for all matrices. Median of a sorted matrix is calculated as: 1. When n is odd median is mat[n/2][n/2] 2. When n is even, median is average of middle two elements. Middle two elements can be found at indexes a[(n-2)/2][n-1] and a[n/2]
If given matrix is unsorted, we can find its median by first sorting the matrix.
Mean : 8.5 Median : 8.5
This article is contributed by Himanshu Ranjan. 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.
- Find median in row wise sorted matrix
- Geometric Median
- Maximize the median of an array
- Median after K additional integers
- Program for Mean and median of an unsorted array
- Median of two sorted arrays with different sizes in O(log(min(n, m)))
- Randomized Algorithms | Set 3 (1/2 Approximate Median)
- Median and Mode using Counting Sort
- Find median of BST in O(n) time and O(1) space
- Median of Stream of Running Integers using STL
- Median of two sorted arrays of same size
- Median of two sorted arrays of different sizes
- Finding Mean, Median, Mode in Python without libraries
- Finding Median in a Sorted Linked List
Improved By : KRV