3
public int[] constructDistancedSequence(int n) {4
int[] ans = new int[n * 2 - 1];5
boolean[] visited = new boolean[n + 1];6
calc(0, ans, visited, n);10
private boolean calc(int index, int[] ans, boolean[] visited, int n) {11
if (index == ans.length) {16
index + 1, ans, visited,17
n); // value already assigned in this position. So go ahead with the next index.19
// we start from n to 1 since we need to find out the lexicographically largest sequence.20
for (int i = n; i >= 1; i--) {21
if (visited[i]) continue;25
if (calc(index + 1, ans, visited, n)) return true;26
} else if (index + i < ans.length && ans[index + i] == 0) {28
i; // assigning the second occurence of i in the desired position i.e, (current index30
if (calc(index + 1, ans, visited, n))31
return true; // largest possible sequence satisfying the given conditions found.