D. Roy Loves Permutation
Time Limit: 1.5 s
Memory Limit: 256.0 MB
Description
You are given a permutation of integers from \(1\) to \(N\). Your task is to rearrange the permutation in such a way that for every consecutive pair of elements \(A_i\) and \(A_{i+1}\) in the array \(A[]\)( \(1 \leq i < N\)) the following two conditions are satisfied:
- \(\gcd(A_i, A_{i+1}) = 1\)
- \(|A_i - A_{i+1}| > 1\)
You must print any such valid rearrangement of the numbers from 1 to N if it exists.
If no such rearrangement exists, print -1
.
Note : gcd means greatest common divisor, for example gcd(2,4)=2, gcd(15,10)=5 etc.
'| |' means absolute value, for example |2-4| = |-2|= 2.
Input
- The first line contains an integer \(T\)(\(1 \leq T \leq 100\) ) — the number of test cases.
- Each of the next \(T\) lines contains a single integer \(N\)(\(2 \leq N \leq 2000\)) — the length of the permutation.
- Sum of \(N\) overall test cases doesn't exceed 2000.
Output
- For each test case, print:
- A line containing \(N\) space-separated integers — a valid rearrangement of permutation \(N\). If multiple valid rearrangements exist, you are allowed to print any of them.
- if no such arrangement exists print
-1
.
Sample
Input | Output |
---|---|
|
|
Information
- ID
- 1203
- Difficulty
- 8
- Category
- (None)
- Tags
- (None)
- # Submissions
- 32
- Accepted
- 3
- Accepted Ratio
- 9%
- Uploaded By
Related
In following contests: