Back to Solutions

Permutations

Explanation

In this problem, we are told to construct a beautiful permutation of n numbers, which is to construct a permutation such that the absolute difference between any two adjacent numbers should not be 1.

This can simply be done by first printing out all the even numbers till n in increasing order, then print out all the odd numbers in increasing order.

Except, for n = 2, 3 this problem has no solution and we need to print NO SOLUTION.

Code

n = int(input())

if n in [2, 3]:
    print("NO SOLUTION")
else:
    for i in range(2, n + 1, 2):
        print(i, end=" ")
    for i in range(1, n + 1, 2):
        print(i, end=" ")
    print()