Find Kth Bit in Nth Binary String LeetCode solution in JavaScript
1var findKthBit = function (n, k) {2if (k === 1) return "0";3if (k === 2) return "1";45let binary = 2;6while (binary < k) {7binary *= 2;8}910let count = 0;11while (binary > 2) {12if (k == binary / 2) {13// The starting number is 1 if at position power of 2 (2^x)14return count % 2 == 0 ? "1" : "0";15}16if (k > binary / 2) {17k = binary - k;18count++;19}20binary /= 2;21}2223return count % 2 == 0 ? "0" : "1";24};