Pair with maximum sum in a Matrix
Last Updated :
05 Sep, 2022
Given a NxM matrix with N rows and M columns of positive integers. The task is to find the sum of pair with maximum sum in the matrix.
Examples:
Input : mat[N][M] = {{1, 2, 3, 4},
{25, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}}
Output : 41
Pair (25, 16) has the maximum sum
Input : mat[N][M] = {{1, 2, 3},
{4, 6, 7},
{9, 10, 5}}
Output : 19
Simple Approach: A simple approach is to traverse the matrix twice and find the first maximum and second maximum elements and return their sum.
Better Approach: A better approach is to find the first and second maximum in a single traversal of the matrix.
1) Initialize two variables first and second to INT_MIN as,
first = second = INT_MIN
2) Start traversing the matrix,
a) If the current element in array say mat[i][j] is greater
than first. Then update first and second as,
second = first
first = mat[i][j]
b) If the current element is in between first and second,
then update second to store the value of current variable as
second = mat[i][j]
3) Return sum of both first and second maximum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 4 // Rows
#define M 4 // Columns
int maxSumPair( int mat[N][M])
{
int max1 = INT_MIN;
int max2 = INT_MIN;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
if (mat[i][j] > max1) {
max2 = max1;
max1 = mat[i][j];
}
else if (mat[i][j] > max2 && mat[i][j] <= max1) {
max2 = mat[i][j];
}
}
}
return max1 + max2;
}
int main()
{
int mat[N][M] = { { 1, 2, 3, 4 },
{ 25, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
cout << maxSumPair(mat) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int N = 4 ;
static int M = 4 ;
static int maxSumPair( int [][]mat)
{
int max1 = Integer.MIN_VALUE;
int max2 = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < M; j++) {
if (mat[i][j] > max1) {
max2 = max1;
max1 = mat[i][j];
}
else if (mat[i][j] > max2 && mat[i][j] <= max1) {
max2 = mat[i][j];
}
}
}
return max1 + max2;
}
public static void main (String[] args) {
int [][]mat = { { 1 , 2 , 3 , 4 },
{ 25 , 6 , 7 , 8 },
{ 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 16 } };
System.out.println(maxSumPair(mat));
}
}
|
Python3
import sys
N = 4
M = 4
def maxSumPair(mat):
max1 = - sys.maxsize - 1
max2 = - sys.maxsize - 1
for i in range ( 0 , N):
for j in range ( 0 , M):
if (mat[i][j] > max1):
max2 = max1
max1 = mat[i][j]
elif (mat[i][j] > max2 and
mat[i][j] < = max1):
max2 = mat[i][j]
return max1 + max2
mat = [ [ 1 , 2 , 3 , 4 ],
[ 25 , 6 , 7 , 8 ],
[ 9 , 10 , 11 , 12 ],
[ 13 , 14 , 15 , 16 ]]
print (maxSumPair(mat))
|
C#
using System;
public class GFG {
static int N = 4;
static int M = 4;
static int maxSumPair( int [,]mat)
{
int max1 = int .MinValue;
int max2 = int .MinValue;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
if (mat[i,j] > max1) {
max2 = max1;
max1 = mat[i,j];
}
else if (mat[i,j] > max2 && mat[i,j] <= max1) {
max2 = mat[i,j];
}
}
}
return max1 + max2;
}
public static void Main () {
int [,]mat = { { 1, 2, 3, 4 },
{ 25, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
Console.WriteLine(maxSumPair(mat));
}
}
|
PHP
<?php
$N = 4;
$M = 4;
function maxSumPair( $mat )
{
global $N ;
global $M ;
$max1 = PHP_INT_MIN;
$max2 = PHP_INT_MIN;
for ( $i = 0; $i < $N ; $i ++)
{
for ( $j = 0; $j < $M ; $j ++)
{
if ( $mat [ $i ][ $j ] > $max1 )
{
$max2 = $max1 ;
$max1 = $mat [ $i ][ $j ];
}
else if ( $mat [ $i ][ $j ] > $max2 &&
$mat [ $i ][ $j ] <= $max1 )
{
$max2 = $mat [ $i ][ $j ];
}
}
}
return $max1 + $max2 ;
}
$mat = array ( array (1, 2, 3, 4 ),
array (25, 6, 7, 8 ),
array (9, 10, 11, 12 ),
array (13, 14, 15, 16 ));
echo maxSumPair( $mat );
?>
|
Javascript
<script>
var N = 4;
var M = 4;
function maxSumPair(mat)
{
var max1 = -1000000000;
var max2 = -1000000000;
for ( var i = 0; i < N; i++) {
for ( var j = 0; j < M; j++) {
if (mat[i][j] > max1) {
max2 = max1;
max1 = mat[i][j];
}
else if (mat[i][j] > max2 && mat[i][j] <= max1) {
max2 = mat[i][j];
}
}
}
return max1 + max2;
}
var mat = [ [ 1, 2, 3, 4 ],
[ 25, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16 ] ];
document.write(maxSumPair(mat));
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...