Decode XORed Permutation LeetCode solution in C++

1
class Solution {
2
public:
3
vector<int> decode(vector<int> &encoded) {
4
int n = encoded.size(), x = 0;
5
// XOR of the permutation
6
for (int i = 1; i <= n + 1; i++) x ^= i;
7

8
// Xoring X with all the odd positioned elements to find first number
9
for (int i = 1; i < n; i += 2) x ^= encoded[i];
10

11
vector<int> res;
12
res.push_back(x);
13
for (int i = 0; i < n; i++) {
14
x ^= encoded[i];
15
res.push_back(x);
16
}
17
return res;
18
}
19
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0