Absolute Permutation
Find lexicographically smallest permutation P of [1..n] where |P[i] - i| = k for all i (1-indexed), or return -1. Algorithm: For each position i, P[i] must be either i+k or i-k. If k=0, return [1,2,...,n]. If n % (2k) ≠0, impossible. Otherwise, arrange in blocks: positions 1..k use i+k, positions k+1..2k use i-k, repeat. Example: n=4, k=2 → P[1]=3, P[2]=4, P[3]=1, P[4]=2 → Check: |3-1|=2, |4-2|=2, |1-3|=2, |2-4|=2 ✓ → Output: '3 4 1 2'
Input
First line: t (test cases). Next t lines: n k (space-separated).
Output
For each test case, output the permutation as space-separated integers, or -1 if impossible.
Sample
3 2 1 3 0 3 2
Expected
2 1 1 2 3 -1
Loading editor…
No output yet. Run your code to see results.