Maximize sum of diagonal of a matrix by rotating all rows or all columns
Last Updated :
01 Oct, 2021
Given a square matrix, mat[][] of dimensions N * N, the task is find the maximum sum of diagonal elements possible from the given matrix by rotating either all the rows or all the columns of the matrix by a positive integer.
Examples:
Input: mat[][] = { { 1, 1, 2 }, { 2, 1, 2 }, { 1, 2, 2 } }
Output: 6
Explanation:
Rotating all the columns of matrix by 1 modifies mat[][] to { {2, 1, 2}, {1, 2, 2}, {1, 1, 2} }.
Therefore, the sum of diagonal elements of the matrix = 2 + 2 + 2 = 6 which is the maximum possible.
Input: A[][] = { { -1, 2 }, { -1, 3 } }
Output: 2
Approach: The idea is to rotate all the rows and columns of the matrix in all possible ways and calculate the maximum sum obtained. Follow the steps to solve the problem:
- Initialize a variable, say maxDiagonalSum to store the maximum possible sum of diagonal elements the matrix by rotating all the rows or columns of the matrix.
- Rotate all the rows of the matrix by a positive integer in the range [0, N – 1] and update the value of maxDiagonalSum.
- Rotate all the columns of the matrix by a positive integer in the range [0, N – 1] and update the value of maxDiagonalSum.
- Finally, print the value of maxDiagonalSum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 3
int findMaximumDiagonalSumOMatrixf( int A[][N])
{
int maxDiagonalSum = INT_MIN;
for ( int i = 0; i < N; i++) {
int curr = 0;
for ( int j = 0; j < N; j++) {
curr += A[j][(i + j) % N];
}
maxDiagonalSum = max(maxDiagonalSum,
curr);
}
for ( int i = 0; i < N; i++) {
int curr = 0;
for ( int j = 0; j < N; j++) {
curr += A[(i + j) % N][j];
}
maxDiagonalSum = max(maxDiagonalSum,
curr);
}
return maxDiagonalSum;
}
int main()
{
int mat[N][N] = { { 1, 1, 2 },
{ 2, 1, 2 },
{ 1, 2, 2 } };
cout<< findMaximumDiagonalSumOMatrixf(mat);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int N = 3 ;
static int findMaximumDiagonalSumOMatrixf( int A[][])
{
int maxDiagonalSum = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++)
{
int curr = 0 ;
for ( int j = 0 ; j < N; j++)
{
curr += A[j][(i + j) % N];
}
maxDiagonalSum = Math.max(maxDiagonalSum,
curr);
}
for ( int i = 0 ; i < N; i++)
{
int curr = 0 ;
for ( int j = 0 ; j < N; j++)
{
curr += A[(i + j) % N][j];
}
maxDiagonalSum = Math.max(maxDiagonalSum,
curr);
}
return maxDiagonalSum;
}
public static void main(String[] args)
{
int [][] mat = { { 1 , 1 , 2 },
{ 2 , 1 , 2 },
{ 1 , 2 , 2 } };
System.out.println(
findMaximumDiagonalSumOMatrixf(mat));
}
}
|
Python3
import sys
N = 3
def findMaximumDiagonalSumOMatrixf(A):
maxDiagonalSum = - sys.maxsize - 1
for i in range (N):
curr = 0
for j in range (N):
curr + = A[j][(i + j) % N]
maxDiagonalSum = max (maxDiagonalSum,
curr)
for i in range (N):
curr = 0
for j in range (N):
curr + = A[(i + j) % N][j]
maxDiagonalSum = max (maxDiagonalSum,
curr)
return maxDiagonalSum
if __name__ = = "__main__" :
mat = [ [ 1 , 1 , 2 ],
[ 2 , 1 , 2 ],
[ 1 , 2 , 2 ] ]
print (findMaximumDiagonalSumOMatrixf(mat))
|
C#
using System;
class GFG{
static int N = 3;
static int findMaximumDiagonalSumOMatrixf( int [,] A)
{
int maxDiagonalSum = Int32.MinValue;
for ( int i = 0; i < N; i++)
{
int curr = 0;
for ( int j = 0; j < N; j++)
{
curr += A[j, (i + j) % N];
}
maxDiagonalSum = Math.Max(maxDiagonalSum,
curr);
}
for ( int i = 0; i < N; i++)
{
int curr = 0;
for ( int j = 0; j < N; j++)
{
curr += A[(i + j) % N, j];
}
maxDiagonalSum = Math.Max(maxDiagonalSum,
curr);
}
return maxDiagonalSum;
}
public static void Main()
{
int [,] mat = { { 1, 1, 2 },
{ 2, 1, 2 },
{ 1, 2, 2 } };
Console.Write(findMaximumDiagonalSumOMatrixf(mat));
}
}
|
Javascript
<script>
let N = 3;
function findMaximumDiagonalSumOMatrixf(A)
{
let maxDiagonalSum = Number.MIN_VALUE;
for (let i = 0; i < N; i++)
{
let curr = 0;
for (let j = 0; j < N; j++)
{
curr += A[j][(i + j) % N];
}
maxDiagonalSum = Math.max(maxDiagonalSum,
curr);
}
for (let i = 0; i < N; i++)
{
let curr = 0;
for (let j = 0; j < N; j++)
{
curr += A[(i + j) % N][j];
}
maxDiagonalSum = Math.max(maxDiagonalSum,
curr);
}
return maxDiagonalSum;
}
let mat = [[ 1, 1, 2 ],
[ 2, 1, 2 ],
[ 1, 2, 2 ]];
document.write(
findMaximumDiagonalSumOMatrixf(mat));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...