Place N ^ 2 numbers in matrix such that every row has an equal sum | Java 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 Code
public class Numbers { public static int [ ] [ ] solve ( int [ ] [ ] arr , int n ) { int [ ] [ ] ans = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { ans [ i ] [ j ] = arr [ j ] [ ( i + j ) % n ] ; } } return ans ; } public static void show ( int [ ] [ ] arr , int n ) { int [ ] [ ] res = solve ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { System . out . print ( res [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static int [ ] [ ] makeArray ( int n ) { int [ ] [ ] arr = new int [ n ] [ n ] ; int c = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) arr [ i ] [ j ] = c ++ ; } return arr ; } public static void main ( String [ ] args ) { int n = 5 ; int [ ] [ ] arr = makeArray ( n ) ; show ( arr , n ) ; } }