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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to swap upper diagonal elements with lower diagonal elements of matrix.
- Maximum sum of elements in a diagonal parallel to the main diagonal of a given Matrix
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix
- Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal
- Program to check diagonal matrix and scalar matrix
- Program to convert given Matrix to a Diagonal 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
- Construct a square Matrix whose parity of diagonal sum is same as size of matrix
- Squares of Matrix Diagonal Elements
- Reverse Diagonal elements of matrix
- Print all the sub diagonal elements of the given square matrix
- Print all the super diagonal elements of the given square matrix
- Construct a matrix with sum equal to the sum of diagonal elements
- Check if two elements of a matrix are on the same diagonal or not
- Sum of main diagonal elements in a Matrix which are prime
- Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's
- Find sum of all Boundary and Diagonal element of a Matrix
- Find maximum sum from top to bottom row with no adjacent diagonal elements
- Print matrix in diagonal pattern
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.