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 : mat[][] = {
{ 1, 2, 3 },
{ 4, 2, 1 },
{ 5, 6, 7 },
};
Output : Row 3 has max sum 18
The idea is to traverse the matrix row-wise and find the sum of elements in each row and check for every row if current sum is greater than the maximum sum obtained till the current row and update the maximum_sum accordingly.
Below is the implementation of the above approach:
C++
// C++ program to find row 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 row with max sum pair<int, int> colMaxSum(int mat[N][N]) { // Variable to store index of row // with maximum int idx = -1; // Variable to store max sum int maxSum = INT_MIN; // Traverse matrix row wise for (int i = 0; i < N; i++) { int sum = 0; // calculate sum of row for (int j = 0; j < N; j++) { sum += mat[i][j]; } // 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 << "Row " << ans.first + 1 << " has max sum " << ans.second; return 0; } |
Java
// Java program to find row with // max sum in a matrix import java.util.ArrayList; class MaxSum { public static int N; static ArrayList<Integer> colMaxSum(int mat[][]) { // Variable to store index of row // with maximum int idx = -1; // Variable to store maximum sum int maxSum = Integer.MIN_VALUE; // Traverse the matrix row wise for (int i = 0; i < N; i++) { int sum = 0; for (int j = 0; j < N; j++) { sum += mat[i][j]; } // Update maxSum if it is less than // current row sum if (maxSum < sum) { maxSum = sum; // store index idx = i; } } // Arraylist to store values of index // of maximum sum and the maximum sum together ArrayList<Integer> res = new ArrayList<>(); res.add(idx); res.add(maxSum); return res; } // Driver code public static void main(String[] args) { N = 5; 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 }, }; ArrayList<Integer> ans = colMaxSum(mat); System.out.println("Row "+ (ans.get(0)+1)+ " has max sum " + ans.get(1)); } } // This code is contributed by Vivekkumar Singh |
Row 3 has max sum 35
Recommended Posts:
- Find a sub matrix with maximum XOR
- Find column with maximum sum in a Matrix
- Find maximum element of each row in a matrix
- Find maximum element of each column in a matrix
- Program to find the maximum element in a Matrix
- Find alphabet in a Matrix which has maximum number of stars around it
- Find pair with maximum difference in any column of a Matrix
- Find Maximum side length of square in a Matrix
- Find row with maximum and minimum number of zeroes in given Matrix
- Find maximum path length in a binary matrix
- Find pair of rows in a binary matrix that has maximum bit difference
- Find maximum path sum in a 2D matrix when exactly two left moves are allowed
- Find row number of a binary matrix having maximum number of 1s
- Maximum trace possible for any sub-matrix of the given matrix
- Maximum XOR value in matrix
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.
Improved By : Vivekkumar Singh




