
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find row with maximum sum in a Matrix in C++
In this problem, we are given a matrix mat[][] of size N*N. Our task is to Find the row with maximum sum in a Matrix.
Let’s take an example to understand the problem,
Input
mat[][] = { 8, 4, 1, 9 3, 5, 7, 9 2, 4, 6, 8 1, 2, 3, 4 }
Output
Row 2, sum 24
Explanation
Row 1: sum = 8+4+1+9 = 22 Row 2: sum = 3+5+7+9 = 24 Row 3: sum = 2+4+6+8 = 20 Row 4: sum = 1+2+3+4 = 10
Solution Approach
A simple solution to the problem is to find the sum of elements of each row and keep a track of maximum sum. Then after all rows are traversed return the row with maximum sum.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; #define R 4 #define C 4 void findMax1Row(int mat[R][C]) { int maxSumRow = 0, maxSum = -1; int i, index; for (i = 0; i < R; i++) { int sum = 0; for(int j = 0; j < C; j++){ sum += mat[i][j]; } if(sum > maxSum){ maxSum = sum; maxSumRow = i; } } cout<<"Row : "<<(maxSumRow+1)<<" has the maximum sum which is "<<maxSum; } int main() { int mat[R][C] = { {8, 4, 1, 9}, {3, 5, 7, 9}, {2, 4, 6, 8}, {1, 2, 3, 4} }; findMax1Row(mat); return 0; }
Output
Row : 2 has the maximum sum which is 24
- Related Articles
- Find column with maximum sum in a Matrix using C++.
- Find the Pair with a Maximum Sum in a Matrix using C++
- Find maximum element of each row in a matrix in C++
- Maximum sum of elements from each row in the matrix in C++
- Maximum path sum in matrix in C++
- Find row number of a binary matrix having maximum number of 1s in C++
- JavaScript Program to Find maximum element of each row in a matrix
- Find the maximum element of each row in a matrix using Python
- Maximum sum of hour glass in matrix in C++
- Maximum sum rectangle in a 2D matrix | DP-27 in C++
- Maximum sum rectangle in a 2D matrix
- Matrix row sum and column sum using C program
- Find sub-matrix with the given sum in C++
- Find the Pair with Given Sum in a Matrix using C++
- Find pair with maximum difference in any column of a Matrix in C++

Advertisements