Find column with maximum sum in a Matrix
Last Updated :
05 Sep, 2022
Given a N*N matrix. The task is to find the index of column with maximum sum. That is the column whose sum of elements are maximum.
Examples:
Input : mat[][] = {
{ 1, 2, 3, 4, 5 },
{ 5, 3, 1, 4, 2 },
{ 5, 6, 7, 8, 9 },
{ 0, 6, 3, 4, 12 },
{ 9, 7, 12, 4, 3 },
};
Output : Column 5 has max sum 31
Input : mat[][] = {
{ 1, 2, 3 },
{ 4, 2, 1 },
{ 5, 6, 7 },
};
Output : Column 3 has max sum 11
The idea is to traverse the matrix column-wise and find the sum of elements in each column and check for every column if current sum is greater than the maximum sum obtained till the current column and update the maximum_sum accordingly.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 5 // No of rows and column
pair< int , int > colMaxSum( int mat[N][N])
{
int idx = -1;
int maxSum = INT_MIN;
for ( int i = 0; i < N; i++) {
int sum = 0;
for ( int j = 0; j < N; j++) {
sum += mat[j][i];
}
if (sum > maxSum) {
maxSum = sum;
idx = i;
}
}
pair< int , int > res;
res = make_pair(idx, maxSum);
return res;
}
int main()
{
int mat[N][N] = {
{ 1, 2, 3, 4, 5 },
{ 5, 3, 1, 4, 2 },
{ 5, 6, 7, 8, 9 },
{ 0, 6, 3, 4, 12 },
{ 9, 7, 12, 4, 3 },
};
pair< int , int > ans = colMaxSum(mat);
cout << "Column " << ans.first + 1 << " has max sum "
<< ans.second;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static final int N = 5 ;
static class Pair
{
int first , second;
Pair( int f, int s)
{
first = f;
second = s;
}
}
static Pair colMaxSum( int mat[][])
{
int idx = - 1 ;
int maxSum = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++)
{
int sum = 0 ;
for ( int j = 0 ; j < N; j++)
{
sum += mat[j][i];
}
if (sum > maxSum)
{
maxSum = sum;
idx = i;
}
}
Pair res;
res = new Pair(idx, maxSum);
return res;
}
public static void main(String args[])
{
int mat[][] = { { 1 , 2 , 3 , 4 , 5 },
{ 5 , 3 , 1 , 4 , 2 },
{ 5 , 6 , 7 , 8 , 9 },
{ 0 , 6 , 3 , 4 , 12 },
{ 9 , 7 , 12 , 4 , 3 }};
Pair ans = colMaxSum(mat);
System.out.println( "Column " + ( int )(ans.first + 1 ) +
" has max sum " + ans.second);
}
}
|
Python3
N = 5
def colMaxSum(mat):
idx = - 1
maxSum = - 10 * * 9
for i in range (N):
Sum = 0
for j in range (N):
Sum + = mat[j][i]
if ( Sum > maxSum):
maxSum = Sum
idx = i
return idx, maxSum
mat = [[ 1 , 2 , 3 , 4 , 5 ],
[ 5 , 3 , 1 , 4 , 2 ],
[ 5 , 6 , 7 , 8 , 9 ],
[ 0 , 6 , 3 , 4 , 12 ],
[ 9 , 7 , 12 , 4 , 3 ]]
ans, ans0 = colMaxSum(mat)
print ( "Column" , ans + 1 ,
"has max Sum" , ans0)
|
C#
using System;
class GFG
{
static readonly int N = 5;
public class Pair
{
public int first , second;
public Pair( int f, int s)
{
first = f;
second = s;
}
}
static Pair colMaxSum( int [,]mat)
{
int idx = -1;
int maxSum = int .MinValue;
for ( int i = 0; i < N; i++)
{
int sum = 0;
for ( int j = 0; j < N; j++)
{
sum += mat[j, i];
}
if (sum > maxSum)
{
maxSum = sum;
idx = i;
}
}
Pair res;
res = new Pair(idx, maxSum);
return res;
}
public static void Main(String []args)
{
int [,]mat = { { 1, 2, 3, 4, 5 },
{ 5, 3, 1, 4, 2 },
{ 5, 6, 7, 8, 9 },
{ 0, 6, 3, 4, 12 },
{ 9, 7, 12, 4, 3 }};
Pair ans = colMaxSum(mat);
Console.WriteLine( "Column " + ( int )(ans.first + 1) +
" has max sum " + ans.second);
}
}
|
Javascript
<script>
let N = 5;
function colMaxSum(mat)
{
let idx = -1;
let maxSum = Number.MIN_VALUE;
for (let i = 0; i < N; i++)
{
let sum = 0;
for (let j = 0; j < N; j++)
{
sum += mat[j][i];
}
if (sum > maxSum)
{
maxSum = sum;
idx = i;
}
}
let res;
res = [idx, maxSum];
return res;
}
let mat = [[ 1, 2, 3, 4, 5 ],
[ 5, 3, 1, 4, 2 ],
[ 5, 6, 7, 8, 9 ],
[ 0, 6, 3, 4, 12 ],
[ 9, 7, 12, 4, 3 ]];
let ans = colMaxSum(mat);
document.write( "Column " + (ans[0] + 1) +
" has max sum " + ans[1]);
</script>
|
Output
Column 5 has max sum 31
Complexity Analysis
- Time Complexity: O(N*N) where N is the size of each row or column in the matrix.
- Auxiliary Space : O(1)
Share your thoughts in the comments
Please Login to comment...