#C12038. Unique Absolute Difference Permutation
Unique Absolute Difference Permutation
Unique Absolute Difference Permutation
You are given an integer n. Your task is to construct a permutation of the numbers from 1 to n such that the absolute differences between every pair of adjacent numbers are unique. In other words, if the permutation is p = [p1, p2, ..., pn], then the set of differences {|p2 - p1|, |p3 - p2|, ..., |pn - pn-1|} must contain only distinct values. Note that if no such permutation exists, you should output an empty list.
For example:
- For n = 1, output:
1
. - For n = 2, output:
[]
(no valid permutation exists under the problem constraints). - For n = 3, one acceptable answer is:
1 3 2
. - For n = 4, one acceptable answer is:
1 3 2 4
or2 4 1 3
. - For n = 5, one acceptable answer is:
1 4 2 5 3
(other variations such as3 1 4 2 5
or1 5 2 4 3
are also valid).
The uniqueness condition can be formally stated as follows: $$\{d_i = |p_{i+1} - p_i|: 1 \le i \le n-1\} = \{1, 2, \dots, n-1\}.$$
inputFormat
The input consists of a single integer n provided via standard input.
Format: A single line containing the integer n (1 ≤ n ≤ 105).
outputFormat
Output the permutation as space‐separated integers on one line. If no valid permutation exists, output []
(without any extra spaces or characters).
1
1
</p>