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
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.
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Find a Square Matrix such that sum of elements in every row and column is K
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Find an N x N grid whose xor of every row and column is equal
- Construct a matrix such that union of ith row and ith column contains every element from 1 to 2N-1
- Replace every matrix element with maximum of GCD of row or column
- Minimum operations required to make each row and column of matrix equals
- Count positions in Binary Matrix having equal count of set bits in corresponding row and column
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Place N^2 numbers in matrix such that every row has an equal sum
- Count number of islands where every island is row-wise and column-wise separated
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Sum of matrix in which each element is absolute difference of its row and column numbers
- Sum of matrix element where each elements is integer division of row and column
- Sum of middle row and column in Matrix
- Program to find the Sum of each Row and each Column of a Matrix
- Maximum sum of a Matrix where each value is from a unique row and column
- Maximum sum of any submatrix of a Matrix which is sorted row-wise and column-wise
- Construct a Binary Matrix whose sum of each row and column is a Prime Number
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.