# Number of elements greater than modified mean in matrix

Normally mean of matrix is the average of all elements present in matrix. Consider a modified mean as floor of mean of row-wise minimum and column-wise maximum. Row-wise minimum is the minimum element from each row of given matrix and column-wise maximum is the maximum element from each column. Given a matrix of order n*m, find the number of elements greater than new mean obtained.

```mean = floor ( (sum(row-wise Min) + sum (col-wise Max )) /
(row_no. + col_no.) )
```

Examples :

```Input : mat[][] = {1, 5, 6,
2, 3, 0,
5, 2, 8}
Output : 4

Input : mat[][] = {1, 5,
5, 2}
Output : 2
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Find sum of all row-wise minimum and sum of all column-wise maximum. Take the mean of this sum by dividing the sum value with (n+m) i.e., total number of rows and columns. Now, as we have the mean of row-wise minimum and column-wise maximum, iterate over the matrix to find the number of elements greater than calculated mean.

Below is the implementation of above approach:

## C++

 `// CPP program to count number of ` `// elements greater than mean ` `#include ` `using` `namespace` `std; ` ` `  `// define constant for row and column ` `#define n 4 ` `#define m 5 ` ` `  `// function to count elements  ` `// greater than mean ` `int` `countElements(``int` `mat[][m]) ` `{ ` `    ``// For each row find minimum  ` `    ``// element and add to rowSum ` `    ``int` `rowSum = 0; ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``int` `min = mat[i]; ` `        ``for` `(``int` `j = 1; j < m; j++) ` `            ``if` `(mat[i][j] < min) ` `                ``min = mat[i][j]; ` `        ``rowSum += min; ` `    ``} ` `     `  `    ``// For each column find maximum  ` `    ``// element and add to colSum ` `    ``int` `colSum = 0; ` `    ``for` `(``int` `i = 0; i < m; i++) { ` `        ``int` `max = mat[i]; ` `        ``for` `(``int` `j = 1; j < n; j++) ` `            ``if` `(max < mat[j][i]) ` `                ``max = mat[j][i]; ` `        ``colSum += max; ` `    ``} ` ` `  `    ``// Calculate mean of row-wise ` `    ``// minimum and column wise maximum ` `    ``int` `mean = (rowSum + colSum) / (m + n); ` `     `  `    ``// For whole matrix count  ` `    ``// elements greater than mean ` `    ``int` `count = 0; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``for` `(``int` `j = 0; j < m; j++) ` `            ``if` `(mean < mat[i][j]) ` `                ``count++; ` `    ``return` `count; ` `} ` ` `  `// driver function ` `int` `main() ` `{ ` `    ``int` `mat[n][m] = { 5, 4, 2, 8, 7, ` `                      ``1, 5, 8, 3, 4, ` `                      ``8, 5, 4, 6, 0, ` `                      ``2, 5, 8, 9, 4 }; ` `    ``cout << countElements(mat); ` `    ``return` `0; ` `} `

## Java

 `// Java program to count ` `// number of elements  ` `// greater than mean ` `class` `GFG { ` `     `  `    ``static` `int` `n = ``4``, m = ``5``; ` `     `  `    ``// function to count  ` `    ``// elements greater ` `    ``// than mean ` `    ``static` `int` `countElements(``int` `mat[][]) ` `    ``{ ` `        ``// For each row find ` `        ``// minimum element  ` `        ``// and add to rowSum ` `        ``int` `rowSum = ``0``; ` `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``{ ` `            ``int` `min = mat[i][``0``]; ` `         `  `            ``for` `(``int` `j = ``1``; j < m; j++) ` `                ``if` `(mat[i][j] < min) ` `                    ``min = mat[i][j]; ` `             `  `            ``rowSum += min; ` `        ``} ` `         `  `        ``// For each column  ` `        ``// find maximum  ` `        ``// element and add ` `        ``// to colSum ` `        ``int` `colSum = ``0``; ` `        ``for` `(``int` `i = ``0``; i < m; i++) { ` `            ``int` `max = mat[``0``][i]; ` `         `  `            ``for` `(``int` `j = ``1``; j < n; j++) ` `                ``if` `(max < mat[j][i]) ` `                    ``max = mat[j][i]; ` `         `  `            ``colSum += max; ` `        ``} ` `     `  `        ``// Calculate mean of ` `        ``// row-wise minimum  ` `        ``// and column wise ` `        ``// maximum ` `        ``int` `mean = (rowSum + colSum) / (m + n); ` `         `  `        ``// For whole matrix  ` `        ``// count elements  ` `        ``// greater than mean ` `        ``int` `count = ``0``; ` `        ``for` `(``int` `i = ``0``; i < n; i++) ` `            ``for` `(``int` `j = ``0``; j < m; j++) ` `         `  `                ``if` `(mean < mat[i][j]) ` `                    ``count++; ` `         `  `        ``return` `count; ` `    ``} ` `     `  `    ``// driver function ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int` `mat[][] = {{ ``5``, ``4``, ``2``, ``8``, ``7``},  ` `                        ``{``1``, ``5``, ``8``, ``3``, ``4``}, ` `                        ``{``8``, ``5``, ``4``, ``6``, ``0``}, ` `                        ``{``2``, ``5``, ``8``, ``9``, ``4``}}; ` `        ``System.out.println(countElements(mat)); ` `    ``} ` `} ` ` `  `// This article is contribute by Prerna Saini. `

## C#

 `// C# program to count number of  ` `// elements greater than mean ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``static` `int` `n = 4, m = 5; ` `     `  `    ``// function to count elements ` `    ``// greater than mean ` `    ``static` `int` `countElements(``int` `[,]mat) ` `    ``{ ` `        ``// For each row find minimum ` `        ``// element and add to rowSum ` `        ``int` `rowSum = 0; ` `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{ ` `            ``int` `min = mat[i,0]; ` `         `  `            ``for` `(``int` `j = 1; j < m; j++) ` `                ``if` `(mat[i,j] < min) ` `                    ``min = mat[i,j]; ` `             `  `            ``rowSum += min; ` `        ``} ` `         `  `        ``// For each column find maximum  ` `        ``// element and add to colSum ` `        ``int` `colSum = 0; ` `        ``for` `(``int` `i = 0; i < m; i++) { ` `            ``int` `max = mat[0,i]; ` `         `  `            ``for` `(``int` `j = 1; j < n; j++) ` `                ``if` `(max < mat[j,i]) ` `                    ``max = mat[j,i]; ` `         `  `            ``colSum += max; ` `        ``} ` `     `  `        ``// Calculate mean of row-wise minimum  ` `        ``// and column wise maximum ` `        ``int` `mean = (rowSum + colSum) / (m + n); ` `         `  `        ``// For whole matrix count  ` `        ``// elements greater than mean ` `        ``int` `count = 0; ` `        ``for` `(``int` `i = 0; i < n; i++) ` `            ``for` `(``int` `j = 0; j < m; j++) ` `         `  `                ``if` `(mean < mat[i,j]) ` `                    ``count++; ` `         `  `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver function ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `[,]mat = {{5, 4, 2, 8, 7},  ` `                      ``{1, 5, 8, 3, 4}, ` `                      ``{8, 5, 4, 6, 0}, ` `                      ``{2, 5, 8, 9, 4}}; ` `    ``Console.Write(countElements(mat)); ` `    ``} ` `} ` ` `  `// This article is contribute by nitin mittal. `

## PHP

 ` `

Output:

```11
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : nitin mittal, vt_m

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.