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++

filter_none

editclose

play_arrow

linkbrightness_4code

// C++ program to find column with

// max sum in a matrix

#include <bits/stdc++.h>

usingnamespacestd;

#define N 5 // No of rows and column

// Function to find the column with max sum

pair<int, int> colMaxSum(intmat[N][N])

{

// Variable to store index of column

// with maximum

intidx = -1;

// Variable to store max sum

intmaxSum = INT_MIN;

// Traverse matrix column wise

for(inti = 0; i < N; i++) {

intsum = 0;

// calculate sum of column

for(intj = 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

returnres;

}

// driver code

intmain()

{

intmat[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;

return0;

}

chevron_right

filter_none

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// Java program to find column

// with max sum in a matrix

importjava.util.*;

classGFG

{

// No of rows and column

staticfinalintN = 5;

// structure for pair

staticclassPair

{

intfirst , second;

Pair(intf, ints)

{

first = f;

second = s;

}

}

// Function to find the column

// with max sum

staticPair colMaxSum(intmat[][])

{

// Variable to store index of

// column with maximum

intidx = -1;

// Variable to store max sum

intmaxSum = Integer.MIN_VALUE;

// Traverse matrix column wise

for(inti = 0; i < N; i++)

{

intsum = 0;

// calculate sum of column

for(intj = 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 = newPair(idx, maxSum);

// return result

returnres;

}

// Driver code

publicstaticvoidmain(String args[])

{

intmat[][] = { { 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

chevron_right

filter_none

Python3

filter_none

editclose

play_arrow

linkbrightness_4code

# Python3 program to find column with

# max Sum in a matrix

N =5

# Function to find the column with max Sum

defcolMaxSum(mat):

# Variable to store index of column

# with maximum

idx =-1

# Variable to store max Sum

maxSum =-10**9

# Traverse matrix column wise

fori inrange(N):

Sum=0

# calculate Sum of column

forj inrange(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

returnidx, 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

chevron_right

filter_none

C#

filter_none

editclose

play_arrow

linkbrightness_4code

// C# program to find column

// with max sum in a matrix

usingSystem;

classGFG

{

// No of rows and column

staticreadonlyintN = 5;

// structure for pair

publicclassPair

{

publicintfirst , second;

publicPair(intf, ints)

{

first = f;

second = s;

}

}

// Function to find the column

// with max sum

staticPair colMaxSum(int[,]mat)

{

// Variable to store index of

// column with maximum

intidx = -1;

// Variable to store max sum

intmaxSum = int.MinValue;

// Traverse matrix column wise

for(inti = 0; i < N; i++)

{

intsum = 0;

// calculate sum of column

for(intj = 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 = newPair(idx, maxSum);

// return result

returnres;

}

// Driver code

publicstaticvoidMain(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

chevron_right

filter_none

Output:

Column 5 has max sum 31

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.