Given an N * N matrix, the task is to find the product of the elements of left and right diagonal.
Input: arr = 1 2 3 4 5 6 7 8 9 7 4 2 2 2 2 1 Output: 9408 Explanation: Product of left diagonal = 1 * 4 * 6 * 1 = 24 Product of right diagonal = 4 * 7 * 7 * 2 = 392 Total product = 24 * 392 = 9408 Input: arr = 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 Output : 512 Explanation: Product of left diagonal = 2 * 2 * 2 * 2 * 2 = 32 Product of right diagonal = 2 * 2 * 2 * 2 * 2 = 32 But we have a common element in this case so Total product = (32 * 32)/2 = 512
- We need to find out the principal diagonal and seconddary diagonal elements of the matrix. Please refer this article for this [ Program to print the Diagonals of a Matrix ]
- In this method we use one loop i.e. a loop for calculating product of both the principal and secondary diagonals
- Divide the answer by middle element for matrix of odd size
Below is the implementation of the above approach:
Time Complexity: O(N)
- Program to swap upper diagonal elements with lower diagonal elements of matrix.
- Program to convert the diagonal elements of the matrix to 0
- Find the sum of the diagonal elements of the given N X N spiral matrix
- Program to check diagonal matrix and scalar matrix
- Program to convert given Matrix to a Diagonal Matrix
- Reverse Diagonal elements of matrix
- Squares of Matrix Diagonal Elements
- Print all the sub diagonal elements of the given square matrix
- Print all the super diagonal elements of the given square matrix
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix
- Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's
- Find maximum sum from top to bottom row with no adjacent diagonal elements
- Maximum product of 4 adjacent elements in matrix
- Program to print elements of a Matrix row-wise skipping alternate elements
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.