Given a number N, place numbers from the range [1, N2] in an NxN matrix such that sum in every row is equal.
Input: N = 3 Output: 1 5 9 2 6 7 3 4 8 Sum in 1st row: 15 Sum in 2nd row: 15 Sum in 2nd row: 15 Input: N = 5 Output: 1 7 13 19 25 2 8 14 20 21 3 9 15 16 22 4 10 11 17 23 5 6 12 18 24
A Greedy Approach has been used to fill the matrix, where the insertion of elements in the matrix is done row-wise. The required matrix can be obtained using below steps:
- Fill the matrix initially with numbers in the range [1, N2] using matrix traversal.
- Traverse the matrix and change every position in a new matrix considering it as answer matrix by answer[i][j] = mat[j][(i+j)%n].
Below is the implementation of the above approach:
1 7 13 19 25 2 8 14 20 21 3 9 15 16 22 4 10 11 17 23 5 6 12 18 24
- In-place convert matrix in specific order
- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
- Count entries equal to x in a special matrix
- Find the largest area rectangular sub-matrix whose sum is equal to k
- Largest area rectangular sub-matrix with equal number of 1's and 0's
- Finding the maximum square sub-matrix with all equal elements
- Find number of transformation to make two Matrix Equal
- Count elements smaller than or equal to x in a sorted matrix
- Minimum operations of given type to make all elements of a matrix equal
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Split N^2 numbers into N groups of equal sum
- Frequencies of even and odd numbers in a matrix
- Print numbers in matrix diagonal pattern
- Minimum cost to sort a matrix of numbers from 0 to n^2 - 1
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 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 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.