📜  与矩阵中的最大和配对

📅  最后修改于: 2022-05-13 01:57:24.641000             🧑  作者: Mango

与矩阵中的最大和配对

给定一个 NxM 矩阵,具有 N 行和 M 列的正整数。任务是找到矩阵中具有最大和的对的和。
例子

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

简单的方法:一个简单的方法是遍历矩阵两次并找到第一个最大和第二个最大元素并返回它们的总和。
更好的方法:更好的方法是在矩阵的单次遍历中找到第一个和第二个最大值。

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.

下面是上述方法的实现:

C++
// C++ program to find maximum sum
// pair in a matrix
 
#include 
using namespace std;
 
#define N 4 // Rows
#define M 4 // Columns
 
// Function to find maximum sum
// pair from matrix
int maxSumPair(int mat[N][M])
{
    int max1 = INT_MIN; // First max
    int max2 = INT_MIN; // Second max
 
    // Traverse the matrix
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (mat[i][j] > max1) {
                max2 = max1; // second max = first max
                max1 = mat[i][j]; // first max = current
            }
            // If second max is between current element
            // and first max
            else if (mat[i][j] > max2 && mat[i][j] <= max1) {
                max2 = mat[i][j];
            }
        }
    }
 
    return max1 + max2;
}
 
// Driver Code
int main()
{
 
    // matrix
    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
// Java program to find maximum sum
// pair in a matrix
import java.io.*;
 
class GFG {
    
static int  N = 4; // Rows
static int M = 4; // Columns
 
// Function to find maximum sum
// pair from matrix
static int maxSumPair(int [][]mat)
{
    int max1 = Integer.MIN_VALUE; // First max
    int max2 = Integer.MIN_VALUE; // Second max
 
    // Traverse the matrix
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (mat[i][j] > max1) {
                max2 = max1; // second max = first max
                max1 = mat[i][j]; // first max = current
            }
            // If second max is between current element
            // and first max
            else if (mat[i][j] > max2 && mat[i][j] <= max1) {
                max2 = mat[i][j];
            }
        }
    }
 
    return max1 + max2;
}
 
// Driver Code
 
    public static void main (String[] args) {
            // matrix
    int [][]mat = { { 1, 2, 3, 4 },
                    { 25, 6, 7, 8 },
                    { 9, 10, 11, 12 },
                    { 13, 14, 15, 16 } };
 
    System.out.println(maxSumPair(mat));
    }
}
// This code is contributed
// by shs


Python3
# Python 3 program to find maximum sum
# pair in a matrix
import sys
 
N = 4 # Rows
M = 4 # Columns
 
# Function to find maximum sum
# pair from matrix
def maxSumPair(mat):
 
    max1 = -sys.maxsize - 1 # First max
    max2 = -sys.maxsize - 1 # Second max
 
    # Traverse the matrix
    for i in range(0, N):
        for j in range (0, M):
            if (mat[i][j] > max1):
                max2 = max1 # second max = first max
                max1 = mat[i][j] # first max = current
             
            # If second max is between current
            # element and first max
            elif (mat[i][j] > max2 and
                  mat[i][j] <= max1):
                max2 = mat[i][j]
             
    return max1 + max2
 
# Driver Code
 
# matrix
mat = [ [1, 2, 3, 4 ],
        [25, 6, 7, 8 ],
        [9, 10, 11, 12 ],
        [13, 14, 15, 16 ]]
 
print(maxSumPair(mat))
 
# This code is contributed
# by ihritik


C#
// C# program to find maximum sum
// pair in a matrix
using System;
public class GFG {
     
static int  N = 4; // Rows
static int M = 4; // Columns
  
// Function to find maximum sum
// pair from matrix
static int maxSumPair(int [,]mat)
{
    int max1 = int.MinValue; // First max
    int max2 = int.MinValue; // Second max
  
    // Traverse the matrix
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (mat[i,j] > max1) {
                max2 = max1; // second max = first max
                max1 = mat[i,j]; // first max = current
            }
            // If second max is between current element
            // and first max
            else if (mat[i,j] > max2 && mat[i,j] <= max1) {
                max2 = mat[i,j];
            }
        }
    }
  
    return max1 + max2;
}
  
// Driver Code
  
    public static void Main () {
            // matrix
    int [,]mat = { { 1, 2, 3, 4 },
                    { 25, 6, 7, 8 },
                    { 9, 10, 11, 12 },
                    { 13, 14, 15, 16 } };
  
    Console.WriteLine(maxSumPair(mat));
    }
}
// This code is contributed by PrinciRaj1992


PHP
 $max1)
            {
                $max2 = $max1; // second max = first max
                $max1 = $mat[$i][$j]; // first max = current
            }
             
            // If second max is between current
            // element and first max
            else if ($mat[$i][$j] > $max2 &&
                     $mat[$i][$j] <= $max1)
            {
                $max2 = $mat[$i][$j];
            }
        }
    }
 
    return $max1 + $max2;
}
 
// Driver Code
 
// matrix
$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);
 
// This code is contributed
// by ihritik
?>


Javascript


输出:
41

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程