Circular Permutation in Binary Representation LeetCode solution in JavaScript

1
var circularPermutation = function (n, start) {
2
const grayCodes = [];
3

4
let startIdx = -1;
5

6
for (let i = 0; i <= 2 ** n - 1; i++) {
7
grayCodes[i] = i ^ (i >> 1);
8

9
if (grayCodes[i] == start) startIdx = i;
10
}
11

12
const res = [];
13

14
for (let i = 0; i <= 2 ** n - 1; i++) {
15
res[i] = grayCodes[startIdx];
16

17
startIdx++;
18

19
if (startIdx == grayCodes.length) startIdx = 0;
20
}
21

22
return res;
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0