1734. Decode XORed Permutation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public int[] decode(int[] encoded) {
int[] res = new int[encoded.length + 1];

int tmp = 0;
for (int i = 0; i < encoded.length; i += 2) {
tmp ^= encoded[i];
}

int last = tmp;
for (int num = 1; num <= encoded.length + 1; num++) {
last ^= num;
}
res[res.length - 1] = last;
for (int i = res.length - 2; i >= 0; i--) {
res[i] = res[i + 1] ^ encoded[i];
}

return res;
}
}

References

1734. Decode XORed Permutation