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