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
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < vector < int > > solve ( vector < vector < int > > arr , int n ) { vector < vector < int > > ans ( n , vector < int > ( n , 0 ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { ans [ i ] [ j ] = arr [ j ] [ ( i + j ) % n ] ; } } return ans ; } void show ( vector < vector < int > > arr , int n ) { vector < vector < int > > res = solve ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { cout << res [ i ] [ j ] << " ▁ " ; } cout << endl ; } } vector < vector < int > > makeArray ( int n ) { vector < vector < int > > arr ( n , vector < int > ( n , 0 ) ) ; int c = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { arr [ i ] [ j ] = c ; c ++ ; } } return arr ; } int main ( ) { int n = 5 ; vector < vector < int > > arr = makeArray ( n ) ; show ( arr , n ) ; return 0 ; }