- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this article, we will discuss finding a pair with a maximum sum in a given matrix or 2-D array. For example

Input : matrix[m][n] = { { 3, 5, 2 }, { 2, 6, 47 }, { 1, 64, 66 } } Output : 130 Explanation : maximum sum is 130 from element pair 64 and 66. Input : matrix[m][n] = { { 55, 22, 46 }, { 6, 2, 1 }, { 3, 24, 52 } } Output : 107 Explanation : maximum sum is 130 from element pair 55 and 52.

Let’s have a brief explanation of the different procedures to solve the given problem without having any issues.

A Brute-force approach can be applied, i.e., initialize the MAX variable with the sum of the first two elements and then traverse through the array and checksum of every pair if it is more significant than MAX MAX the new sum value. But this process will take more time with the time complexity of O((m*n)2).

An efficient approach can be applied, i.e., initialize two-variable MAX1 and MAX2 with 0 and then traverse through the 2-D array; check whether the current element is more significant than MAX1. If yes, then replace MAX2 with MAX1 and MAX1 with the existing part. In this way, we will be able to find two maximum numbers, and obviously, the sum of two whole numbers will be the maximum.

#include <bits/stdc++.h> using namespace std; int main() { int m = 3, n = 3; // initialising matrix with values int matrix[m][n] = { { 55, 22, 46 }, { 6, 2, 1 }, { 3, 24, 52 } }; // initialising MAX1 and MAX2 to keep two maximum numbers. int MAX1 = INT_MIN; int MAX2 = INT_MIN; int result; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { // check if the element is greater than MAX1. if (matrix[i][j] > MAX1) { MAX2 = MAX1; MAX1 = matrix[i][j]; } // check if the current element is between MAX1 and MAX2. else if (matrix[i][j] > MAX2 && matrix[i][j] <= MAX1) { MAX2 = matrix[i][j]; } } } // calculating maximum sum by adding both maximum numbers. result = MAX1 + MAX2; cout << "maximum sum in Matrix : " << result ; return 0; }

maximum sum in Matrix : 107

- Storing elements in the 2-D array and initializing MAX1 and MAX2 with a minimum value of INT.
- Traversing through the matrix.
- If the current part is more significant than MAX1, then replace MAX2 with MAX1 and MAX1 with the current element.
- If the present piece is more reduced than MAX1 and more meaningful than MAX2, then replace MAX2 with the current element.

- Calculate the result by adding two MAX1 and MAX2 and print the work.

In this article, we discussed finding a pair with a maximum sum in a given matrix. We discussed the approach of finding a solution and also discussed C++ code for the same. We can write this code in any other language like Java, C, Python, etc. we hope you find this article helpful.

- Related Questions & Answers
- Find the Pair with Given Sum in a Matrix using C++
- Find column with maximum sum in a Matrix using C++.
- Find row with maximum sum in a Matrix in C++
- Find pair with maximum difference in any column of a Matrix in C++
- Find a specific pair in Matrix in C++
- Find a pair with given sum in BST in C++
- Find a pair with given sum in a Balanced BST in C++
- Find a pair from the given array with maximum nCr value in C++
- Find a pair with maximum product in array of Integers in C++
- Find pair of rows in a binary matrix that has maximum bit difference in C++
- Find a pair with the given difference in C++
- Maximum sum rectangle in a 2D matrix
- Find required sum pair with JavaScript
- Find a pair with given sum in a Balanced BST in Java
- Find a pair with given sum in a Balanced BST in Python

Advertisements