
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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][0]; 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[0][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
- Related Articles
- Python – Sort Matrix by Number of elements greater than its previous element
- Program to find out the number of consecutive elements in a matrix whose gcd is greater than 1 in Python
- Find Elements Greater Than a Given Number In a Subarray in Java
- Find the number of elements greater than k in a sorted array using C++
- Python - Number of values greater than K in list
- How to calculate the number of elements greater than a certain value in a vector in R?
- Finding element greater than its adjacent elements in JavaScript
- Find the Number of segments where all elements are greater than X using C++
- Find Array Elements Which are Greater than its Left Elements in Java?
- Kth prime number greater than N in C++
- Count subarrays with all elements greater than K in C++
- Mask array elements greater than a given value in Numpy
- Smallest prime number just greater than the specified number in JavaScript
- How to find the mean of a square matrix elements by excluding diagonal elements in R?
- Retaining array elements greater than cumulative sum using reduce() in JavaScript
