Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

Matrix Construction

By Arghya · Limits 1s, 512 MB · Custom Checker

Construct a square matrix aa of size n×nn×n satisfying the following conditions,

  1. Each row of the matrix is a permutation of size nn
  2. Each column of the matrix is a permutation of size nn
  3. The matrix is symmetric, that means a[i][j]=a[j][i]a[i][j] = a[j][i] for all possible i,ji,j (1i,jn)(1≤i,j≤n)
  4. The matrix satisfies a[i][i]=ia[i][i] = i for all ii (1in)(1≤i≤n)

If it is possible to build such a matrix, print the matrix otherwise print 1-1.


First line of input consists of a single integer TT, the number of test cases (1T5)(1 ≤ T ≤ 5)
Each test case consists of one line. First and only line of the test case contains nn, the dimension of the matrix. (1n1000)(1 ≤ n ≤ 1000).


Print the matrix if possible, otherwise print 1-1. If there are multiple possible answers, anyone would be acceptable.


1 3 2
3 2 1
2 1 3

We call a sequence of integers AA permutation if every integer from 1 to |A| appears exactly once in the sequence, where A|A| denotes the length of the sequence.



    100% Solution Ratio

    Tahmid690Earliest, 1d ago

    Tahmid690Fastest, 0.2s

    Tahmid690Lightest, 16 MB

    Tahmid690Shortest, 963B


    Login to submit