#C8914. Deranged Array Generation

    ID: 52949 Type: Default 1000ms 256MiB

Deranged Array Generation

Deranged Array Generation

Given an integer ( N ), generate an array ( A ) which is a permutation of ( {1, 2, \ldots, N} ) such that for every index ( i ) (with 1-based indexing), the condition ( A_i \neq i ) holds. In other words, no element in the array is in its original position. If no valid derangement exists (i.e. when ( N = 1 )), output (-1).

The task is to ensure that each integer from 1 to ( N ) appears exactly once in the array and that the derangement property is satisfied. The output should be printed in a single line as space-separated integers for each test case.

inputFormat

The input begins with a single integer ( T ) indicating the number of test cases. Then ( T ) lines follow, each containing a single integer ( N ) representing the size of the array to be generated.

For example:

4 1 2 3 4

outputFormat

For each test case, output a single line. If a valid derangement exists, print the deranged array as space-separated integers. Otherwise, print (-1) if ( N = 1 ).## sample

4
1
2
3
4
-1

2 1 2 3 1 2 3 4 1

</p>