Find the Pair with a Maximum Sum in a Matrix using C++


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.

Approach to find The Solution

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

Brute-force Approach

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).

Efficient Approach

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.

Example

#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;
}

Output

maximum sum in Matrix : 107

Explanation of the above code

  • 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.

Conclusion

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.

Updated on: 26-Nov-2021

158 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements