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++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
#define N 3
// Function to find maximum sum of diagonal elements
// of matrix by rotating either rows or columns
int findMaximumDiagonalSumOMatrixf(int A[][N])
{
// Stores maximum diagonal sum of elements
// of matrix by rotating rows or columns
int maxDiagonalSum = INT_MIN;
// Rotate all the columns by an integer
// in the range [0, N - 1]
for (int i = 0; i < N; i++) {
// Stores sum of diagonal elements
// of the matrix
int curr = 0;
// Calculate sum of diagonal
// elements of the matrix
for (int j = 0; j < N; j++) {
// Update curr
curr += A[j][(i + j) % N];
}
// Update maxDiagonalSum
maxDiagonalSum = max(maxDiagonalSum,
curr);
}
// Rotate all the rows by an integer
// in the range [0, N - 1]
for (int i = 0; i < N; i++) {
// Stores sum of diagonal elements
// of the matrix
int curr = 0;
// Calculate sum of diagonal
// elements of the matrix
for (int j = 0; j < N; j++) {
// Update curr
curr += A[(i + j) % N][j];
}
// Update maxDiagonalSum
maxDiagonalSum = max(maxDiagonalSum,
curr);
}
return maxDiagonalSum;
}
// Driver code
int main()
{
int mat[N][N] = { { 1, 1, 2 },
{ 2, 1, 2 },
{ 1, 2, 2 } };
cout<< findMaximumDiagonalSumOMatrixf(mat);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
static int N = 3;
// Function to find maximum sum of
// diagonal elements of matrix by
// rotating either rows or columns
static int findMaximumDiagonalSumOMatrixf(int A[][])
{
// Stores maximum diagonal sum of elements
// of matrix by rotating rows or columns
int maxDiagonalSum = Integer.MIN_VALUE;
// Rotate all the columns by an integer
// in the range [0, N - 1]
for(int i = 0; i < N; i++)
{
// Stores sum of diagonal elements
// of the matrix
int curr = 0;
// Calculate sum of diagonal
// elements of the matrix
for(int j = 0; j < N; j++)
{
// Update curr
curr += A[j][(i + j) % N];
}
// Update maxDiagonalSum
maxDiagonalSum = Math.max(maxDiagonalSum,
curr);
}
// Rotate all the rows by an integer
// in the range [0, N - 1]
for(int i = 0; i < N; i++)
{
// Stores sum of diagonal elements
// of the matrix
int curr = 0;
// Calculate sum of diagonal
// elements of the matrix
for(int j = 0; j < N; j++)
{
// Update curr
curr += A[(i + j) % N][j];
}
// Update maxDiagonalSum
maxDiagonalSum = Math.max(maxDiagonalSum,
curr);
}
return maxDiagonalSum;
}
// Driver Code
public static void main(String[] args)
{
int[][] mat = { { 1, 1, 2 },
{ 2, 1, 2 },
{ 1, 2, 2 } };
System.out.println(
findMaximumDiagonalSumOMatrixf(mat));
}
}
// This code is contributed by susmitakundugoaldanga
Python3
# Python3 program to implement
# the above approach
import sys
N = 3
# Function to find maximum sum of diagonal
# elements of matrix by rotating either
# rows or columns
def findMaximumDiagonalSumOMatrixf(A):
# Stores maximum diagonal sum of elements
# of matrix by rotating rows or columns
maxDiagonalSum = -sys.maxsize - 1
# Rotate all the columns by an integer
# in the range [0, N - 1]
for i in range(N):
# Stores sum of diagonal elements
# of the matrix
curr = 0
# Calculate sum of diagonal
# elements of the matrix
for j in range(N):
# Update curr
curr += A[j][(i + j) % N]
# Update maxDiagonalSum
maxDiagonalSum = max(maxDiagonalSum,
curr)
# Rotate all the rows by an integer
# in the range [0, N - 1]
for i in range(N):
# Stores sum of diagonal elements
# of the matrix
curr = 0
# Calculate sum of diagonal
# elements of the matrix
for j in range(N):
# Update curr
curr += A[(i + j) % N][j]
# Update maxDiagonalSum
maxDiagonalSum = max(maxDiagonalSum,
curr)
return maxDiagonalSum
# Driver code
if __name__ == "__main__":
mat = [ [ 1, 1, 2 ],
[ 2, 1, 2 ],
[ 1, 2, 2 ] ]
print(findMaximumDiagonalSumOMatrixf(mat))
# This code is contributed by chitranayal
C#
// C# program to implement
// the above approach
using System;
class GFG{
static int N = 3;
// Function to find maximum sum of
// diagonal elements of matrix by
// rotating either rows or columns
static int findMaximumDiagonalSumOMatrixf(int[,] A)
{
// Stores maximum diagonal sum of elements
// of matrix by rotating rows or columns
int maxDiagonalSum = Int32.MinValue;
// Rotate all the columns by an integer
// in the range [0, N - 1]
for(int i = 0; i < N; i++)
{
// Stores sum of diagonal elements
// of the matrix
int curr = 0;
// Calculate sum of diagonal
// elements of the matrix
for(int j = 0; j < N; j++)
{
// Update curr
curr += A[j, (i + j) % N];
}
// Update maxDiagonalSum
maxDiagonalSum = Math.Max(maxDiagonalSum,
curr);
}
// Rotate all the rows by an integer
// in the range [0, N - 1]
for(int i = 0; i < N; i++)
{
// Stores sum of diagonal elements
// of the matrix
int curr = 0;
// Calculate sum of diagonal
// elements of the matrix
for(int j = 0; j < N; j++)
{
// Update curr
curr += A[(i + j) % N, j];
}
// Update maxDiagonalSum
maxDiagonalSum = Math.Max(maxDiagonalSum,
curr);
}
return maxDiagonalSum;
}
// Driver Code
public static void Main()
{
int[,] mat = { { 1, 1, 2 },
{ 2, 1, 2 },
{ 1, 2, 2 } };
Console.Write(findMaximumDiagonalSumOMatrixf(mat));
}
}
// This code is contributed by code_hunt
JavaScript
<script>
// Javascript program to implement
// the above approach
let N = 3;
// Function to find maximum sum of
// diagonal elements of matrix by
// rotating either rows or columns
function findMaximumDiagonalSumOMatrixf(A)
{
// Stores maximum diagonal sum of elements
// of matrix by rotating rows or columns
let maxDiagonalSum = Number.MIN_VALUE;
// Rotate all the columns by an integer
// in the range [0, N - 1]
for(let i = 0; i < N; i++)
{
// Stores sum of diagonal elements
// of the matrix
let curr = 0;
// Calculate sum of diagonal
// elements of the matrix
for(let j = 0; j < N; j++)
{
// Update curr
curr += A[j][(i + j) % N];
}
// Update maxDiagonalSum
maxDiagonalSum = Math.max(maxDiagonalSum,
curr);
}
// Rotate all the rows by an integer
// in the range [0, N - 1]
for(let i = 0; i < N; i++)
{
// Stores sum of diagonal elements
// of the matrix
let curr = 0;
// Calculate sum of diagonal
// elements of the matrix
for(let j = 0; j < N; j++)
{
// Update curr
curr += A[(i + j) % N][j];
}
// Update maxDiagonalSum
maxDiagonalSum = Math.max(maxDiagonalSum,
curr);
}
return maxDiagonalSum;
}
// Driver Code
let mat = [[ 1, 1, 2 ],
[ 2, 1, 2 ],
[ 1, 2, 2 ]];
document.write(
findMaximumDiagonalSumOMatrixf(mat));
// This code is contributed by souravghosh0416.
</script>
Time Complexity: O(N2)
Auxiliary Space: O(1)
Similar Reads
Javascript Program to Maximize sum of diagonal of a matrix by rotating all rows or all columns 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:
3 min read
Maximize sum by selecting M elements from the start or end of rows of a Matrix Given a 2D array Blocks[][] consisting of N rows of variable length. The task is to select at most M elements with the maximum possible sum from Blocks[][] from either the start or end of a row. Examples: Input: N = 3, M = 4 Blocks[][] = {{2, 3, 5}, {-1, 7}, {8, 10}}Output: 30Explanation: Select {5}
11 min read
Maximize score of Binary Matrix by Flipping a row or column each time Given a Binary matrix of size MxN, the task is to maximize the score of Matrix by making any number of moves (including zero moves), such that, every row is interpreted as a Binary Number, and the score of the matrix is the sum of these binary numbers. A move is defined as choosing any row or column
8 min read
Maximize count of rows consisting of equal elements by flipping columns of a Matrix Given a binary matrix, mat[][] of dimensions N * M, the task is to maximize the count of rows consisting only of equal elements by selecting any column of the matrix and flipping all the elements of that column in each operation. Print the maximum number of rows that can be made to form equal elemen
9 min read
Maximum sum of any submatrix of a Matrix which is sorted row-wise and column-wise Given a matrix mat[][] whose elements are sorted both row-wise and column-wise. The task is to find the maximum sum of any submatrix from the given matrix mat[][]. Examples: Input: mat[][] = { {-6, -4, -1}, {-3, 2, 4}, {2, 5, 8}} Output: 19 Explanation: The largest submatrix is given by: 2 4 5 8Inpu
10 min read