Given 9 elements in a 3 x 3 matrix where the value of diagonals are 0. We need to find the values in diagonal to make the sum of every row, column and diagonal equal.
Input: 0 3 6 5 0 5 4 7 0 Output: 6 3 6 5 5 5 4 7 4 Explanation: Now the value of the sum of any row or column is 15 Input: 0 4 4 4 0 4 4 4 0 Output: 4 4 4 4 4 4 4 4 4
- Let’s say the diagonal is x, y and z.
- The value of x will be ( x2, 3 + x3, 2 ) / 2.
- The value of z will be ( x1, 2 + x2, 1 ) / 2.
- The value of y will be ( x + z ) / 2.
Below is the implementation of the above approach:
Matrix initially: 0 54 48 36 0 78 66 60 0 Matrix with diagonals: 69 54 48 36 34 78 66 60 45
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Program to swap upper diagonal elements with lower diagonal elements of matrix.
- Program to check diagonal matrix and scalar matrix
- Mirror of matrix across diagonal
- Squares of Matrix Diagonal Elements
- Print matrix in diagonal pattern
- Sum of non-diagonal parts of a square Matrix
- Sort the major diagonal of the matrix
- Zigzag (or diagonal) traversal of Matrix
- Reverse Diagonal elements of matrix
- Program to convert given Matrix to a Diagonal Matrix
- Program to convert the diagonal elements of the matrix to 0
- Print numbers in matrix diagonal pattern
- Find the sum of the diagonal elements of the given N X N spiral matrix
- Print all the sub diagonal elements of the given square matrix
- Find sum of all Boundary and Diagonal element of a Matrix
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.