All combinations of elements satisfying given constraints

Given a positive number N, find all combinations of 2*N elements such that every element from 1 to N appears exactly twice and distance between its two appearances is exactly equal to value of the element.

Note that no combination of elements is possible for some values of N like 2, 5, 6, etc.

We can use backtracking to solve this problem. The idea is to try all possible combinations for the first element and recursively explore remaining elements to check if they will lead to the solution or not. If current configuration doesn’t result in solution, we backtrack. Note that an element k can be placed at position i and (i+k+1) in the output array where i >= 0 and (i + k + 1) < 2*N.