https://www.geeksforgeeks.org/fill-missing-entries-of-a-magic-square/
Given a 3X3 matrix mat with it’s left diagonal elements missing (set to 0), considering the sum of every row, column and diagonal of the original matrix was equal, the task is to find the missing diagonal elements and print the original matrix.
Examples:
Input: mat[][] = {{0, 7, 6}, {9, 0, 1}, {4, 3, 0}}
Output:
2 7 6
9 5 1
4 3 8
Row sum = Column sum = Diagonal sum = 15Input: mat[][] = {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}}
Output:
1 1 1
1 1 1
1 1 1
Approach: Let Sum denote the total sum excluding the diagonal elements,
Sum = total sum of the given matrix – diagonalSum
Sum = (3 * rowSum) – diagonalSum
Sum = (2 * rowSum) [Since, columnSum = rowSum = diagonalSum]
rowSum = Sum / 2
Hence, we can insert an element in every row such that the sum of the row is rowSum