Absolute Permutation
medium⏱ 15 mintypescriptchallengesproblem-solving-intermediate-
Runs in your browser
- Before starting
- Use a supported browser; initial dependencies may need internet.
- What it proves
- Run shows output; Test checks the authored cases, not every possible input.
- When finished
- Stop active runs when finished. No cloud infrastructure is provisioned by this exercise.
LEARN BY DOINGPut your intuition to work3 steps
Absolute Permutation
Use the lesson’s examples and explanation to check your idea.
Personal reflection. Nothing is sent to an AI service.Before you start
Browser lesson checks; review each requirement in Tests.
Catalog level: Medium · Estimated time: 15 min. Your prior experience can change the difficulty.
2 1
1 2 3
-1
Sample case — your program reads stdin and prints to stdout.
Need a hand?
Start with a small test case. Hints progress from an idea to an explanation and, when requested, code.
Starter 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 format
First line: t (test cases). Next t lines: n k (space-separated).
Output format
For each test case, output the permutation as space-separated integers, or -1 if impossible.
Constraints
1 ≤ t ≤ 10, 1 ≤ n ≤ 10⁵, 0 ≤ k < n