Place N ^ 2 numbers in matrix such that every row has an equal sum | C # 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
using System ; class GFG { 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 ; } 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 ++ ) { Console . Write ( res [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } 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 ; } static void Main ( ) { int n = 5 ; int [ , ] arr = makeArray ( n ) ; show ( arr , n ) ; } }