-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path1734-decode-xored-permutation.js
More file actions
40 lines (35 loc) · 1010 Bytes
/
1734-decode-xored-permutation.js
File metadata and controls
40 lines (35 loc) · 1010 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 1734. Decode XORed Permutation
* https://leetcode.com/problems/decode-xored-permutation/
* Difficulty: Medium
*
* There is an integer array perm that is a permutation of the first n positive integers,
* where n is always odd.
*
* It was encoded into another integer array encoded of length n - 1, such that
* encoded[i] = perm[i] XOR perm[i + 1]. For example, if perm = [1,3,2], then encoded = [2,1].
*
* Given the encoded array, return the original array perm. It is guaranteed that the answer
* exists and is unique.
*/
/**
* @param {number[]} encoded
* @return {number[]}
*/
var decode = function(encoded) {
const n = encoded.length + 1;
let totalXor = 0;
for (let i = 1; i <= n; i++) {
totalXor ^= i;
}
let oddXor = 0;
for (let i = 1; i < encoded.length; i += 2) {
oddXor ^= encoded[i];
}
const result = new Array(n);
result[0] = totalXor ^ oddXor;
for (let i = 0; i < n - 1; i++) {
result[i + 1] = result[i] ^ encoded[i];
}
return result;
};