Place N ^ 2 numbers in matrix such that every row has an equal sum | Javascript program to distribute n ^ 2 numbers to n people ; 2D array for storing the final result ; using modulo to go to the firs column after the last column ; making a 2D array containing numbers ; driver program
function solve ( arr , n ) { let ans = new Array ( n ) ; for ( var i = 0 ; i < ans . length ; i ++ ) { ans [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { ans [ i ] [ j ] = arr [ j ] [ ( i + j ) % n ] ; } } return ans ; } function show ( arr , n ) { let res = solve ( arr , n ) ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { document . write ( res [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } function makeArray ( n ) { let arr = new Array ( n ) ; for ( var i = 0 ; i < arr . length ; i ++ ) { arr [ i ] = new Array ( 2 ) ; } let c = 1 ; for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) arr [ i ] [ j ] = c ++ ; } return arr ; } let n = 5 ; let arr = makeArray ( n ) ; show ( arr , n ) ;