Place N ^ 2 numbers in matrix such that every row has an equal sum | Python3 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
def solve ( arr , n ) : NEW_LINE INDENT ans = [ [ 0 for i in range ( n ) ] for j in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT ans [ i ] [ j ] = arr [ j ] [ ( i + j ) % n ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def show ( arr , n ) : NEW_LINE INDENT res = solve ( arr , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT print ( res [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def makeArray ( n ) : NEW_LINE INDENT arr = [ [ 0 for i in range ( n ) ] for j in range ( n ) ] NEW_LINE c = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT arr [ i ] [ j ] = c NEW_LINE c += 1 NEW_LINE DEDENT DEDENT return arr NEW_LINE DEDENT n = 5 NEW_LINE arr = makeArray ( n ) NEW_LINE show ( arr , n ) NEW_LINE