Absolute Permutation ⬀
We define P to be a permutation of the first n natural numbers in the range [1, n]. Let pos[i] denote the value at position i in permutation P using 1-based indexing.
P is considered to be an absolute permutation if |pos[i] - i| = k holds true for every i ∈ [1, n].
Given n and k, print the lexicographically smallest absolute permutation P. If no absolute permutation exists, print -1.
n = 4k = 2
Create an array of elements from 1 to n, pos = [1, 2, 3, 4]. Using 1 based indexing, create a permutation where every |pos[i] - i| = k. It can be rearranged to [3, 4, 1, 2] so that all of the absolute differences equal k = 2:
pos[i] i |pos[i] - i|
3 1 2
4 2 2
1 3 2
2 4 2
Complete the absolutePermutation function in the editor below.
absolutePermutation has the following parameter(s):
int n: the upper bound of natural numbers to consider, inclusiveint k: the absolute difference between each element's value and its index
int[n]: the lexicographically smallest permutation, or[-1]if there is none
- The first line contains an integer
t, the number of queries. - Each of the next
tlines contains2space-separated integers,nandk.
1 ≤ t < 101 ≤ n ≤ 10⁵0 ≤ k < n
STDIN Function
----- --------
3 t = 3 (number of queries)
2 1 n = 2, k = 1
3 0 n = 3, k = 0
3 2 n = 3, k = 2
2 1
1 2 3
-1
No absolute permutation exists, so we print -1 on a new line.

