Java Program to Maximize sum of diagonal of a matrix by rotating all rows or all columns Last Updated : 27 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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: 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 Output: 6 Time Complexity: O(N2) Auxiliary Space: O(1) Please refer complete article on Maximize sum of diagonal of a matrix by rotating all rows or all columns for more details! Comment More infoAdvertise with us Next Article Java Program to Maximize sum of diagonal of a matrix by rotating all rows or all columns K kartik Follow Improve Article Tags : Java Greedy Matrix Java Programs DSA rotation +2 More Practice Tags : GreedyJavaMatrix Similar Reads Java Program to Compute the Sum of Diagonals of a Matrix For a given 2D square matrix of size N*N, our task is to find the sum of elements in the Principal and Secondary diagonals. For example, analyze the following 4 Ã 4 input matrix.m00 m01 m02 m03m10 m11 m12 m13m20 m21 m22 m23m30 m31 m32 m33Basic Input/Output:Input 1: 6 7 3 4 8 9 2 1 1 2 9 6 6 5 7 2Out 4 min read Java Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row. Examples: Input : [1, 2, 3] [1, 4, 9] [76, 34, 21] Output : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56 Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each ele 2 min read Java Program for Maximum sum of i*arr[i] among all rotations of a given array Given an array arr[] of n integers, find the maximum that maximizes the sum of the value of i*arr[i] where i varies from 0 to n-1. Examples: Input: arr[] = {8, 3, 1, 2} Output: 29 Explanation: Lets look at all the rotations, {8, 3, 1, 2} = 8*0 + 3*1 + 1*2 + 2*3 = 11 {3, 1, 2, 8} = 3*0 + 1*1 + 2*2 + 6 min read Java Program to Efficiently compute sums of diagonals of a matrix Given a 2D square matrix, find the sum of elements in Principal and Secondary diagonals. For example, consider the following 4 X 4 input matrix. A00 A01 A02 A03 A10 A11 A12 A13 A20 A21 A22 A23 A30 A31 A32 A33 The primary diagonal is formed by the elements A00, A11, A22, A33. Condition for Principal 3 min read Java Program to Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed Given an array, only rotation operation is allowed on array. We can rotate the array as many times as we want. Return the maximum possible summation of i*arr[i]. Examples :Â Â Input: arr[] = {1, 20, 2, 10} Output: 72 We can get 72 by rotating array twice. {2, 10, 1, 20} 20*3 + 1*2 + 10*1 + 2*0 = 72 I 4 min read Like