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

📅  最后修改于: 2021-04-22 07:45:50             🧑  作者: Mango

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

例子:

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

  • 将变量maxSum初始化为0 ,该变量将存储最大对角线总和。
  • i遍历[0,N – 1]的0行的列。
  • 初始化变量sum1sum2 ,它们将分别存储从单元格(行,col)和从单元格(col,行)开始的对角线总和其中r为0,c为col
  • rowc都增加1 。当rowcol小于N时,mat [row] [col]添加到sum1并将mat [col] [row]添加sum2 。最后,更新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 )