Problem link: CSES — Distinct sums grids
Problem statement:
Create an $$$n \times n$$$ grid that fulfills the following requirements:
$$$1. $$$ Each integer $$$1 \dots n$$$ appears n times in the grid.
$$$2. $$$ If we create a set that consists of all sums in rows and columns, there are 2n distinct values.
My solution:
For a given $$$n \gt 4$$$, create a $$$n \times m$$$ matrix named A, every element in A will be as follow:
For example, n = 6 would look like this:
Now it can be seen that for each row $$$i$$$ from 1 to n, its sum is as follow: $$$row_{i} = 2 \times n - 1 + (n - 2) \times (i - 1)$$$
For each column from 2 to n, its sum is as follow: $$$column_{i} = \frac{n \times (n - 1)}{2} + i - 1$$$. And for the first columns, its sum is $$$n ^ 2$$$
This construction give us a powerful fact that is there can only be one row's sum being equal to a column's sum, and from the third to last row onward, there can not be a match between these and any column. Also the matching column can not be the second and the last. This is true for $$$n \gt 4$$$ and will be proven later in this blog.
From this we can do the following procedure: Find the matching row and column $$$i, j$$$, swap $$$a_{2, 1}, a_{2, j}$$$, and then swap $$$a_{i + 1, n}, a_{i + 2, n}$$$



