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++ program to implement // the above approach #include<bits/stdc++.h> using namespace std;
#define N 3 #define M 3 // Function to count the number of rows // whose sum exceeds the sum of // elements of the remaining matrix void countRows( int mat[M][N])
{ // To store the result
int count = 0;
// Stores the total sum of
// the matrix elements
int totalSum = 0;
// Calculate the total sum
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < M; j++)
{
totalSum += mat[i][j];
}
}
// Traverse to check for each row
for ( int i = 0; i < N; i++)
{
// Stores the sum of elements
// of the current row
int currSum = 0;
// Calculate the sum of elements
// of the current row
for ( int j = 0; j < M; j++)
{
currSum += mat[i][j];
}
// If sum of current row exceeds
// the sum of rest of the matrix
if (currSum > totalSum - currSum)
// Increase count
count++;
}
// Print the result
cout << count;
} // Driver Code int main()
{ // Given matrix
int mat[N][M] = {{2, -1, 5},
{-3, 0, -2},
{5, 1, 2}};
// Function Call
countRows(mat);
} // This code is contributed by Rajput-Ji |
// Java program to implement // the above approach import java.io.*;
class GFG {
// Function to count the number of rows
// whose sum exceeds the sum of
// elements of the remaining matrix
static void countRows( int [][] mat)
{
// Stores the matrix dimensions
int n = mat.length;
int m = mat[ 0 ].length;
// To store the result
int count = 0 ;
// Stores the total sum of
// the matrix elements
int totalSum = 0 ;
// Calculate the total sum
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < m; j++) {
totalSum += mat[i][j];
}
}
// Traverse to check for each row
for ( int i = 0 ; i < n; i++) {
// Stores the sum of elements
// of the current row
int currSum = 0 ;
// Calculate the sum of elements
// of the current row
for ( int j = 0 ; j < m; j++) {
currSum += mat[i][j];
}
// If sum of current row exceeds
// the sum of rest of the matrix
if (currSum > totalSum - currSum)
// Increase count
count++;
}
// Print the result
System.out.println(count);
}
// Driver Code
public static void main(String[] args)
{
// Given matrix
int [][] mat
= { { 2 , - 1 , 5 },
{ - 3 , 0 , - 2 },
{ 5 , 1 , 2 } };
// Function Call
countRows(mat);
}
} |
# Python3 program to implement # the above approach # Function to count the number of rows # whose sum exceeds the sum of elements # of the remaining matrix def countRows(mat):
# Stores the matrix dimensions
n = len (mat)
m = len (mat[ 0 ])
# To store the result
count = 0
# Stores the total sum of
# the matrix elements
totalSum = 0
# Calculate the total sum
for i in range (n):
for j in range (m):
totalSum + = mat[i][j]
# Traverse to check for each row
for i in range (n):
# Stores the sum of elements
# of the current row
currSum = 0
# Calculate the sum of elements
# of the current row
for j in range (m):
currSum + = mat[i][j]
# If sum of current row exceeds
# the sum of rest of the matrix
if (currSum > totalSum - currSum):
# Increase count
count + = 1
# Print the result
print (count)
# Driver Code if __name__ = = '__main__' :
# Given matrix
mat = [ [ 2 , - 1 , 5 ],
[ - 3 , 0 , - 2 ],
[ 5 , 1 , 2 ] ]
# Function call
countRows(mat)
# This code is contributed by mohit kumar 29 |
// C# program to implement // the above approach using System;
class GFG{
// Function to count the number of rows // whose sum exceeds the sum of // elements of the remaining matrix static void countRows( int [,] mat)
{ // Stores the matrix dimensions
int n = mat.GetLength(0);
int m = mat.GetLength(1);
// To store the result
int count = 0;
// Stores the total sum of
// the matrix elements
int totalSum = 0;
// Calculate the total sum
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
totalSum += mat[i, j];
}
}
// Traverse to check for each row
for ( int i = 0; i < n; i++)
{
// Stores the sum of elements
// of the current row
int currSum = 0;
// Calculate the sum of elements
// of the current row
for ( int j = 0; j < m; j++)
{
currSum += mat[i, j];
}
// If sum of current row exceeds
// the sum of rest of the matrix
if (currSum > totalSum - currSum)
// Increase count
count++;
}
// Print the result
Console.WriteLine(count);
} // Driver Code public static void Main(String[] args)
{ // Given matrix
int [,] mat = {{2, -1, 5},
{-3, 0, -2},
{5, 1, 2}};
// Function Call
countRows(mat);
} } // This code is contributed by 29AjayKumar |
<script> // Javascript program to implement // the above approach var N = 3
var M = 3
// Function to count the number of rows // whose sum exceeds the sum of // elements of the remaining matrix function countRows( mat)
{ // To store the result
var count = 0;
// Stores the total sum of
// the matrix elements
var totalSum = 0;
// Calculate the total sum
for ( var i = 0; i < N; i++)
{
for ( var j = 0; j < M; j++)
{
totalSum += mat[i][j];
}
}
// Traverse to check for each row
for ( var i = 0; i < N; i++)
{
// Stores the sum of elements
// of the current row
var currSum = 0;
// Calculate the sum of elements
// of the current row
for ( var j = 0; j < M; j++)
{
currSum += mat[i][j];
}
// If sum of current row exceeds
// the sum of rest of the matrix
if (currSum > totalSum - currSum)
// Increase count
count++;
}
// Print the result
document.write( count);
} // Driver Code // Given matrix var mat = [[2, -1, 5],
[-3, 0, -2],
[5, 1, 2]];
// Function Call countRows(mat); // This code is contributed by itsok. </script> |
2
Time Complexity: O(N * M)
Auxiliary Space: O(1)