Number of elements greater than modified mean in matrix
Normally mean of the matrix is the average of all elements present in the matrix. Consider a modified mean as the floor of the mean of the row-wise minimum and column-wise maximum. The row-wise minimum is the minimum element from each row of the given matrix and the column-wise maximum is the maximum element from each column. Given a matrix of order n*m, find the number of elements greater than the 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
Find the sum of all row-wise minimums and the sum of all column-wise maximums. Take the mean of this sum by dividing the sum value with (n+m) i.e., the total number of rows and columns. Now, as we have the mean of the row-wise minimum and column-wise maximum, iterate over the matrix to find the number of elements greater than the calculated mean
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define n 4
#define m 5
int countElements( int mat[][m])
{
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;
}
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;
}
int mean = (rowSum + colSum) / (m + n);
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;
}
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
class GFG {
static int n = 4 , m = 5 ;
static int countElements( int mat[][])
{
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;
}
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;
}
int mean = (rowSum + colSum) / (m + n);
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;
}
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));
}
}
|
Python3
n = 4 ;
m = 5 ;
def countElements(mat):
rowSum = 0 ;
for i in range (n):
min = mat[i][ 0 ];
for j in range ( 1 , m):
if (mat[i][j] < min ):
min = mat[i][j];
rowSum + = min ;
colSum = 0 ;
for i in range (m):
max = mat[ 0 ][i];
for j in range ( 1 , n):
if ( max < mat[j][i]):
max = mat[j][i];
colSum + = max ;
mean = ((rowSum + colSum) /
(m + n));
count = 0 ;
for i in range (n):
for j in range (m):
if (mean < mat[i][j]):
count + = 1 ;
return count;
if __name__ = = '__main__' :
mat = [[ 5 , 4 , 2 , 8 , 7 ],
[ 1 , 5 , 8 , 3 , 4 ],
[ 8 , 5 , 4 , 6 , 0 ],
[ 2 , 5 , 8 , 9 , 4 ]];
print (countElements(mat));
|
C#
using System;
class GFG {
static int n = 4, m = 5;
static int countElements( int [,]mat)
{
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;
}
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;
}
int mean = (rowSum + colSum) / (m + n);
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;
}
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));
}
}
|
PHP
<?php
$n = 4;
$m = 5;
function countElements( $mat )
{
$rowSum = 0;
global $n , $m ;
for ( $i = 0; $i < $n ; $i ++)
{
$min = $mat [ $i ][0];
for ( $j = 1; $j < $m ; $j ++)
if ( $mat [ $i ][ $j ] < $min )
$min = $mat [ $i ][ $j ];
$rowSum += $min ;
}
$colSum = 0;
for ( $i = 0; $i < $m ; $i ++)
{
$max = $mat [0][ $i ];
for ( $j = 1; $j < $n ; $j ++)
if ( $max < $mat [ $j ][ $i ])
$max = $mat [ $j ][ $i ];
$colSum += $max ;
}
$mean = ( $rowSum + $colSum ) / ( $m + $n );
$count = 0;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = 0; $j < $m ; $j ++)
if ( $mean < $mat [ $i ][ $j ])
$count ++;
return $count ;
}
$mat = array ( array (5, 4, 2, 8, 7),
array (1, 5, 8, 3, 4),
array (8, 5, 4, 6, 0),
array (2, 5, 8, 9, 4));
echo countElements( $mat );
?>
|
Javascript
<script>
var n = 4
var m = 5
function countElements(mat)
{
var rowSum = 0;
for ( var i = 0; i < n; i++) {
var min = mat[i][0];
for ( var j = 1; j < m; j++)
if (mat[i][j] < min)
min = mat[i][j];
rowSum += min;
}
var colSum = 0;
for ( var i = 0; i < m; i++) {
var max = mat[0][i];
for ( var j = 1; j < n; j++)
if (max < mat[j][i])
max = mat[j][i];
colSum += max;
}
var mean = (rowSum + colSum) / (m + n);
var count = 0;
for ( var i = 0; i < n; i++)
for ( var j = 0; j < m; j++)
if (mean < mat[i][j])
count++;
return count;
}
var mat = [ [5, 4, 2, 8, 7],
[1, 5, 8, 3, 4],
[8, 5, 4, 6, 0],
[2, 5, 8, 9, 4 ]];
document.write( countElements(mat));
</script>
|
Time Complexity: O(n*m), where n is the number of rows and m is the number of columns.
Auxiliary Space: O(1)
Last Updated :
20 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...