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++
// C++ program to find column with
// max sum in a matrix
#include <bits/stdc++.h>
using namespace std;
#define N 5 // No of rows and column
// Function to find the column with max sum
pair<int, int> colMaxSum(int mat[N][N])
{
// Variable to store index of column
// with maximum
int idx = -1;
// Variable to store max sum
int maxSum = INT_MIN;
// Traverse matrix column wise
for (int i = 0; i < N; i++) {
int sum = 0;
// calculate sum of column
for (int j = 0; j < N; j++) {
sum += mat[j][i];
}
// Update maxSum if it is less than
// current sum
if (sum > maxSum) {
maxSum = sum;
// store index
idx = i;
}
}
pair<int, int> res;
res = make_pair(idx, maxSum);
// return result
return res;
}
// driver code
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
// Java program to find column
// with max sum in a matrix
import java.util.*;
class GFG
{
// No of rows and column
static final int N = 5;
// structure for pair
static class Pair
{
int first , second;
Pair(int f, int s)
{
first = f;
second = s;
}
}
// Function to find the column
// with max sum
static Pair colMaxSum(int mat[][])
{
// Variable to store index of
// column with maximum
int idx = -1;
// Variable to store max sum
int maxSum = Integer.MIN_VALUE;
// Traverse matrix column wise
for (int i = 0; i < N; i++)
{
int sum = 0;
// calculate sum of column
for (int j = 0; j < N; j++)
{
sum += mat[j][i];
}
// Update maxSum if it is
// less than current sum
if (sum > maxSum)
{
maxSum = sum;
// store index
idx = i;
}
}
Pair res;
res = new Pair(idx, maxSum);
// return result
return res;
}
// Driver code
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);
}
}
// This code is contributed
// by Arnab Kundu
Python3
# Python3 program to find column with
# max Sum in a matrix
N = 5
# Function to find the column with max Sum
def colMaxSum(mat):
# Variable to store index of column
# with maximum
idx = -1
# Variable to store max Sum
maxSum = -10**9
# Traverse matrix column wise
for i in range(N):
Sum = 0
# calculate Sum of column
for j in range(N):
Sum += mat[j][i]
# Update maxSum if it is less
# than current Sum
if (Sum > maxSum):
maxSum = Sum
# store index
idx = i
# return result
return idx, maxSum
# Driver Code
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)
# This code is contributed by
# Mohit kumar 29
C#
// C# program to find column
// with max sum in a matrix
using System;
class GFG
{
// No of rows and column
static readonly int N = 5;
// structure for pair
public class Pair
{
public int first , second;
public Pair(int f, int s)
{
first = f;
second = s;
}
}
// Function to find the column
// with max sum
static Pair colMaxSum(int [,]mat)
{
// Variable to store index of
// column with maximum
int idx = -1;
// Variable to store max sum
int maxSum = int.MinValue;
// Traverse matrix column wise
for (int i = 0; i < N; i++)
{
int sum = 0;
// calculate sum of column
for (int j = 0; j < N; j++)
{
sum += mat[j, i];
}
// Update maxSum if it is
// less than current sum
if (sum > maxSum)
{
maxSum = sum;
// store index
idx = i;
}
}
Pair res;
res = new Pair(idx, maxSum);
// return result
return res;
}
// Driver code
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);
}
}
// This code has been contributed by 29AjayKumar
JavaScript
<script>
// Javascript program to find column
// with max sum in a matrix
// No of rows and column
let N = 5;
// Function to find the column
// with max sum
function colMaxSum(mat)
{
// Variable to store index of
// column with maximum
let idx = -1;
// Variable to store max sum
let maxSum = Number.MIN_VALUE;
// Traverse matrix column wise
for (let i = 0; i < N; i++)
{
let sum = 0;
// calculate sum of column
for (let j = 0; j < N; j++)
{
sum += mat[j][i];
}
// Update maxSum if it is
// less than current sum
if (sum > maxSum)
{
maxSum = sum;
// store index
idx = i;
}
}
let res;
res = [idx, maxSum];
// return result
return res;
}
// Driver code
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]);
// This code is contributed by unknown2108
</script>
OutputColumn 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)
Similar Reads
Find row with maximum sum in a Matrix Given an N*N matrix. The task is to find the index of a row with the maximum sum. That is the row whose sum of elements is 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 : Row 3 has max sum 35 Input :
11 min read
Find Column with Maximum Zeros in Matrix Given a matrix(2D array) M of size N*N consisting of 0s and 1s only. The task is to find the column with the maximum number of 0s. If more than one column exists, print the one which comes first. If the maximum number of 0s is 0 then return -1. Examples: Input: N = 3, M[][] = {{0, 0, 0}, {1, 0, 1},
5 min read
Find Matrix With Given Row and Column Sums Given two arrays rowSum[] and colSum[] of size n and m respectively, the task is to construct a matrix of dimensions n à m such that the sum of matrix elements in every ith row is rowSum[i] and the sum of matrix elements in every jth column is colSum[j].Note: The resultant matrix can have only non-n
15 min read
Pair with maximum sum in a Matrix Given a NxM matrix with N rows and M columns of positive integers. The task is to find the sum of pair with maximum sum in the matrix. Examples: Input : mat[N][M] = {{1, 2, 3, 4}, {25, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}} Output : 41 Pair (25, 16) has the maximum sum Input : mat[N][M] = {{1,
7 min read
Find maximum element of each column in a matrix Given a matrix, the task is to find the maximum element of each column. Examples: Input: [1, 2, 3] [1, 4, 9] [76, 34, 21] Output: 76 34 21 Input: [1, 2, 3, 21] [12, 1, 65, 9] 1, 56, 34, 2] Output: 12 56 65 21 Approach: The idea is to run the loop for no_of_cols. Check each element inside the column
6 min read