Find Kth Bit in Nth Binary String LeetCode solution in JavaScript

1
var findKthBit = function (n, k) {
2
if (k === 1) return "0";
3
if (k === 2) return "1";
4

5
let binary = 2;
6
while (binary < k) {
7
binary *= 2;
8
}
9

10
let count = 0;
11
while (binary > 2) {
12
if (k == binary / 2) {
13
// The starting number is 1 if at position power of 2 (2^x)
14
return count % 2 == 0 ? "1" : "0";
15
}
16
if (k > binary / 2) {
17
k = binary - k;
18
count++;
19
}
20
binary /= 2;
21
}
22

23
return count % 2 == 0 ? "0" : "1";
24
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0