Related Articles

# Place N^2 numbers in matrix such that every row has an equal sum

• Difficulty Level : Hard
• Last Updated : 30 Apr, 2021

Given a number N, place numbers from the range [1, N2] in an NxN matrix such that sum in every row is equal.

Examples:

```Input: N = 3
Output: 1 5 9
2 6 7
3 4 8
Sum in 1st row: 15
Sum in 2nd row: 15
Sum in 2nd row: 15

Input: N = 5
Output: 1 7 13 19 25
2 8 14 20 21
3 9 15 16 22
4 10 11 17 23
5 6 12 18 24```

A Greedy Approach has been used to fill the matrix, where the insertion of elements in the matrix is done row-wise. The required matrix can be obtained using below steps:

• Fill the matrix initially with numbers in the range [1, N2] using matrix traversal.
• Traverse the matrix and change every position in a new matrix considering it as answer matrix by answer[i][j] = mat[j][(i+j)%n].

Below is the implementation of the above approach:

## C++

 `// C++ program to distribute n^2 numbers``// to n people``#include ``using` `namespace` `std;` `vector> solve(vector> arr,``                          ``int` `n)``{``    ` `    ``// 2D array for storing the final result``    ``vector> ans(n, vector<``int``> (n, 0));` `    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ``for``(``int` `j = 0; j < n; j++)``        ``{``            ` `            ``// Using modulo to go to the firs``            ``// column after the last column``            ``ans[i][j] = arr[j][(i + j) % n];``        ``}``    ``}``    ``return` `ans;``}` `void` `show(vector> arr, ``int` `n)``{``    ``vector> res = solve(arr, n);``    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ``for``(``int` `j = 0; j < n; j++)``        ``{``            ``cout << res[i][j] << ``" "``;``        ``}``        ``cout << endl;``    ``}``}` `// Making a 2D array containing numbers``vector> makeArray(``int` `n)``{``    ``vector> 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;``}` `// Driver code``int` `main()``{``    ``int` `n = 5;``    ``vector> arr = makeArray(n);``    ` `    ``show(arr, n);``    ` `    ``return` `0;``}` `// This code is contributed by divyesh072019`

## Java

 `// Java program to distribute n^2 numbers to n people``public` `class` `Numbers {``    ``public` `static` `int``[][] solve(``int``[][] arr, ``int` `n)``    ``{``        ``// 2D array for storing the final result``        ``int``[][] ans = ``new` `int``[n][n];` `        ``for` `(``int` `i = ``0``; i < n; i++) {``            ``for` `(``int` `j = ``0``; j < n; j++) {``                ``// using modulo to go to the firs``                ``// column after the last column``                ``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();``        ``}``    ``}``    ``// making a 2D array containing numbers``    ``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;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `n = ``5``;``        ``int``[][] arr = makeArray(n);``        ``show(arr, n);``    ``}``}`

## Python3

 `# Python3 program to distribute n^2``# numbers to n people``def` `solve(arr, n):``    ` `    ``# 2D array for storing the final result``    ``ans ``=` `[[``0` `for` `i ``in` `range``(n)]``              ``for` `j ``in` `range``(n)]` `    ``for` `i ``in` `range``(n):``        ``for` `j ``in` `range``(n):` `            ``# Using modulo to go to the firs``            ``# column after the last column``            ``ans[i][j] ``=` `arr[j][(i ``+` `j) ``%` `n]` `    ``return` `ans` `def` `show(arr, n):``    ` `    ``res ``=` `solve(arr, n)` `    ``for` `i ``in` `range``(n):``        ``for` `j ``in` `range``(n):``            ``print``(res[i][j], end ``=` `" "``)``            ` `        ``print``()` `# Making a 2D array containing numbers``def` `makeArray(n):``    ` `    ``arr ``=` `[[``0` `for` `i ``in` `range``(n)]``              ``for` `j ``in` `range``(n)]` `    ``c ``=` `1` `    ``for` `i ``in` `range``(n):``        ``for` `j ``in` `range``(n):``            ``arr[i][j] ``=` `c``            ``c ``+``=` `1``            ` `    ``return` `arr` `# Driver Code``n ``=` `5``arr ``=` `makeArray(n)` `show(arr, n)` `# This code is contributed by avanitrachhadiya2155`

## C#

 `// C# program to distribute n^2``// numbers to n people``using` `System;` `class` `GFG{``    ` `static` `int``[,] solve(``int``[,] arr, ``int` `n)``{``    ` `    ``// 2D array for storing the final result``    ``int``[,] ans = ``new` `int``[n, n];` `    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ``for``(``int` `j = 0; j < n; j++)``        ``{``            ` `            ``// Using modulo to go to the firs``            ``// column after the last column``            ``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();``    ``}``}` `// Making a 2D array containing numbers``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;``}` `// Driver code``static` `void` `Main()``{``    ``int` `n = 5;``    ``int``[,] arr = makeArray(n);``    ` `    ``show(arr, n);``}``}` `// This code is contributed by divyeshrabadiya07`

## Javascript

 ``
Output:
```1 7 13 19 25
2 8 14 20 21
3 9 15 16 22
4 10 11 17 23
5 6 12 18 24```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up