Count rows with sum exceeding sum of the remaining Matrix
Last Updated :
19 May, 2021
Given a matrix mat[][] of dimensions N * M, the task is to count the number of rows whose sum exceeds the sum of the remaining elements of the Matrix.
Examples:
Input: mat[][] = {{1, 5}, {2, 3}}
Output: 1
Explanation:
Sum of elements of the first row {1, 5} exceeds the sum of the remaining matrix {2, 3}.
Input: mat[][] = {{2, -1, 5}, {-3, 0, -2}, {5, 1, 2}}
Output: 2
Approach: To solve the problem, the idea is to pre-calculate the total sum of the elements of the given matrix and for every row, check if the sum of elements of the current row is greater than the sum of the elements of the rest of the matrix, i.e., currSum > (totalSum – currSum). For every row satisfying the afore-mentioned condition, increase count. Finally, print the value of count obtained after the complete traversal of the matrix.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
#define N 3
#define M 3
void countRows( int mat[M][N])
{
int count = 0;
int totalSum = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < M; j++)
{
totalSum += mat[i][j];
}
}
for ( int i = 0; i < N; i++)
{
int currSum = 0;
for ( int j = 0; j < M; j++)
{
currSum += mat[i][j];
}
if (currSum > totalSum - currSum)
count++;
}
cout << count;
}
int main()
{
int mat[N][M] = {{2, -1, 5},
{-3, 0, -2},
{5, 1, 2}};
countRows(mat);
}
|
Java
import java.io.*;
class GFG {
static void countRows( int [][] mat)
{
int n = mat.length;
int m = mat[ 0 ].length;
int count = 0 ;
int totalSum = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < m; j++) {
totalSum += mat[i][j];
}
}
for ( int i = 0 ; i < n; i++) {
int currSum = 0 ;
for ( int j = 0 ; j < m; j++) {
currSum += mat[i][j];
}
if (currSum > totalSum - currSum)
count++;
}
System.out.println(count);
}
public static void main(String[] args)
{
int [][] mat
= { { 2 , - 1 , 5 },
{ - 3 , 0 , - 2 },
{ 5 , 1 , 2 } };
countRows(mat);
}
}
|
Python3
def countRows(mat):
n = len (mat)
m = len (mat[ 0 ])
count = 0
totalSum = 0
for i in range (n):
for j in range (m):
totalSum + = mat[i][j]
for i in range (n):
currSum = 0
for j in range (m):
currSum + = mat[i][j]
if (currSum > totalSum - currSum):
count + = 1
print (count)
if __name__ = = '__main__' :
mat = [ [ 2 , - 1 , 5 ],
[ - 3 , 0 , - 2 ],
[ 5 , 1 , 2 ] ]
countRows(mat)
|
C#
using System;
class GFG{
static void countRows( int [,] mat)
{
int n = mat.GetLength(0);
int m = mat.GetLength(1);
int count = 0;
int totalSum = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
totalSum += mat[i, j];
}
}
for ( int i = 0; i < n; i++)
{
int currSum = 0;
for ( int j = 0; j < m; j++)
{
currSum += mat[i, j];
}
if (currSum > totalSum - currSum)
count++;
}
Console.WriteLine(count);
}
public static void Main(String[] args)
{
int [,] mat = {{2, -1, 5},
{-3, 0, -2},
{5, 1, 2}};
countRows(mat);
}
}
|
Javascript
<script>
var N = 3
var M = 3
function countRows( mat)
{
var count = 0;
var totalSum = 0;
for ( var i = 0; i < N; i++)
{
for ( var j = 0; j < M; j++)
{
totalSum += mat[i][j];
}
}
for ( var i = 0; i < N; i++)
{
var currSum = 0;
for ( var j = 0; j < M; j++)
{
currSum += mat[i][j];
}
if (currSum > totalSum - currSum)
count++;
}
document.write( count);
}
var mat = [[2, -1, 5],
[-3, 0, -2],
[5, 1, 2]];
countRows(mat);
</script>
|
Time Complexity: O(N * M)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...