📜  与给定矩阵的主对角线平行的对角线上元素的最大总和

📅  最后修改于: 2021-09-06 05:21:16             🧑  作者: Mango

给出一个维度为N * N 的方阵mat[][] ,任务是沿着平行于主对角线的对角线找到给定矩阵中存在的元素的最大和。下面是相同的图像。

例子:

方法:这个想法是遍历与主对角线平行的每条对角线的单元格,并观察对于主对角线上方从单元格(x, y)开始的任何对角线,主对角线下方的相应对角线将从单元格开始(y, x) 。对于每个对角线,从单元格(x, y)开始,其所有元素都将位于单元格(x + k, y + k) 上,其中0 <= x + k, y + k < N 。请按照以下步骤解决问题:

  • 0初始化一个变量maxSum ,它将存储最大对角线和。
  • 在范围[ – 1 0,N]i横穿0行的列。
  • 初始化变量sum1sum2 ,它们将分别存储从单元格(row, col)和单元格(col, row)开始的对角线和其中r 是 0 并且 c 是 col
  • rowc 都增加1 。将mat[row][col]添加到sum1并将mat[col][row] 添加sum2,rowcol小于N 。最后,更新maxSum以存储maxSum、sum1 和 sum2 的最大值
  • 遍历给定矩阵后,打印值maxSum作为最大和。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to return maximum diagonal
// sum that are parallel to main diagonal
int maxDiagonalSum(vector > arr, int N)
{
    // Initialize maxSum
    int maxSum = 0;
 
    // Traverse through the columns
    for (int i = 0; i < N; i++) {
 
        // Initialize r and c
        int row = 0, col = i;
 
        // Diagonal sums
        int sum1 = 0, sum2 = 0;
        while (col < N && row < N) {
            sum1 += arr[row][col];
            sum2 += arr[col][row];
            row++;
            col++;
        }
 
        // Update maxSum with
        // the maximum sum
        maxSum = max({ sum1, maxSum, sum2 });
    }
 
    // Return the maxSum
    return maxSum;
}
 
// Driver Code
int main()
{
    // Given matrix mat[][]
    vector > mat
        = { { 1, 2, 5, 7 },
            { 2, 6, 7, 3 },
            { 12, 3, 2, 4 },
            { 3, 6, 9, 4 } };
    int N = mat.size();
 
    // Function Call
    cout << maxDiagonalSum(mat, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
   
// Function to return maximum diagonal
// sum that are parallel to main diagonal
static int maxDiagonalSum(int arr[][], int N)
{
     
    // Initialize maxSum
    int maxSum = 0;
 
    // Traverse through the columns
    for(int i = 0; i < N; i++)
    {
         
        // Initialize r and c
        int row = 0, col = i;
 
        // Diagonal sums
        int sum1 = 0, sum2 = 0;
        while (col < N && row < N)
        {
            sum1 += arr[row][col];
            sum2 += arr[col][row];
            row++;
            col++;
        }
 
        // Update maxSum with
        // the maximum sum
        maxSum = Math.max(maxSum,
                          Math.max(sum1,
                                   sum2));
    }
 
    // Return the maxSum
    return maxSum;
}
 
// Driver code
public static void main (String[] args)
{
     
    // Given matrix mat[][]
    int mat[][] = { { 1, 2, 5, 7 },
                    { 2, 6, 7, 3 },
                    { 12, 3, 2, 4 },
                    { 3, 6, 9, 4 } };
    int N = mat.length;
 
    // Function Call
    System.out.println(maxDiagonalSum(mat, N));
}
}
 
// This code is contributed by math_lover


Python3
# Python3 program for the above approach
 
# Function to return maximum diagonal
# sum that are parallel to main diagonal
def maxDiagonalSum(arr, N):
     
    # Initialize maxSum
    maxSum = 0
 
    # Traverse through the columns
    for i in range(N):
         
        # Initialize r and c
        row = 0
        col = i
 
        # Diagonal sums
        sum1 = 0
        sum2 = 0
         
        while col < N and row < N:
            sum1 += arr[row][col]
            sum2 += arr[col][row]
            row += 1
            col += 1
 
        # Update maxSum with
        # the maximum sum
        maxSum = max([ sum1, maxSum, sum2])
 
    # Return the maxSum
    return maxSum
 
# Driver Code
if __name__ == '__main__':
     
    # Given matrix mat[][]
    mat = [ [ 1, 2, 5, 7 ],
            [ 2, 6, 7, 3 ],
            [ 12, 3, 2, 4 ],
            [ 3, 6, 9, 4 ] ]
 
    N = len(mat)
 
    # Function Call
    print(maxDiagonalSum(mat, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the
// above approach
using System;
class GFG{
   
// Function to return maximum
// diagonal sum that are parallel
// to main diagonal
static int maxDiagonalSum(int [,]arr,
                          int N)
{   
  // Initialize maxSum
  int maxSum = 0;
 
  // Traverse through the
  // columns
  for(int i = 0; i < N; i++)
  {
    // Initialize r and c
    int row = 0, col = i;
 
    // Diagonal sums
    int sum1 = 0, sum2 = 0;
    while (col < N && row < N)
    {
      sum1 += arr[row,col];
      sum2 += arr[col,row];
      row++;
      col++;
    }
 
    // Update maxSum with
    // the maximum sum
    maxSum = Math.Max(maxSum,
             Math.Max(sum1,
                      sum2));
  }
 
  // Return the maxSum
  return maxSum;
}
 
// Driver code
public static void Main(String[] args)
{   
  // Given matrix [,]mat
  int [,]mat = {{1, 2, 5, 7},
                {2, 6, 7, 3},
                {12, 3, 2, 4},
                {3, 6, 9, 4}};
  int N = mat.GetLength(0);
 
  // Function Call
  Console.WriteLine(maxDiagonalSum(mat, N));
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出
18

时间复杂度: O(N 2 )
辅助空间: O(N 2 )

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live