# Number of elements greater than modified mean in matrix in C++

The modified mean of the matrix is defined as follows...

(sum(row-wise min) + sum(column-wise max)) / (row_size + column_size)

Let's see an example.

1 2 3
4 5 6
7 8 9

mean = (sum(1 + 4 + 7) + sum(7 + 8 + 9)) / (3 + 3)

We have to find the mean first and then count the number of elements that are greater than mean.

If we take the above example, then we will get 3 as the count. There are 3 elements that are greater than the mean which is 6.

## Algorithm

• Initialise the matrix.

• Find the row-wise minimum elements sum.

• Find the column-wise maximum elements sum.

• Then find the mean with above mentioned formula.

• Now, count the number of elements greater than the mean of the matrix.

## Implementation

Following is the implementation of the above algorithm in C++

#include <bits/stdc++.h>
using namespace std;
#define m 3
#define n 3
int getElementCountGreaterThanMean(int matrix[][n]) {
int rowSum = 0;
for (int i = 0; i < m; i++) {
int min = matrix[i];
for (int j = 1; j < n; j++) {
if (matrix[i][j] < min){
min = matrix[i][j];
}
}
rowSum += min;
}
int colSum = 0;
for (int i = 0; i < n; i++) {
int max = matrix[i];
for (int j = 1; j < m; j++) {
if (max < matrix[j][i]) {
max = matrix[j][i];
}
}
colSum += max;
}
int mean = (rowSum + colSum) / (m + n);
int count = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mean < matrix[i][j]) {
count++;
}
}
}
return count;
}
int main() {
int matrix[m][n] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
cout << getElementCountGreaterThanMean(matrix) << endl;
return 0;
}

## Output

If you run the above code, then you will get the following result.

3