f=>{var s="[";for(int i=0;i<n*n-1;)s+=i%-~n<1|i++%~-n<1?i+",":"";return s+n*n+"]";}
Try it online!
The change here is based on the shift between numbers to find. The two shifts starting at 0 are n-1
and n+1
, so if n=5
, the numbers for n-1
would be 0,4,8,12,16,20
and for n+1
would be 0,6,12,18,24
. Combining these and giving 1-indexing (instead of 0-indexing) gives 1,5,7,9,13,17,19,21,25
. The offset from n
is achieved using bitwise negation (bitwise complement operation), where ~-n==n-1
and -~n==n+1
.
Old Version
f=>{var s="[";for(int i=0;i<n*n-1;i++)s+=(i/n!=i%n&&n-1-i/n!=i%n?"":i+1+",");return s+$"{n*n}]";}
Try it online!
This approach uses the column and row indices for determining if the numbers are on the diagonals. i/n
gives the row index, and i%n
gives the column index.
Returning Only The Number Array
If constructing only the number array is deemed to count towards the byte cost, then the following could be done, based on Dennis.Verweij's suggestion (using System.Linq;
adds an extra 18 bytes):
C# (.NET Core), 66+18=84 bytes
x=>Enumerable.Range(1,x*x).Where(v=>~-v%~-x<1|~-v%-~x<1).ToArray()
Try it online!