Longest Binary Subsequence Less Than or Equal to K LeetCode solution in JavaScript
1/**2* @param {string} s3* @param {number} k4* @return {number}5*/67var longestSubsequence = function (s, k) {8let count = 0;9let j = s.length - 1; // starting from the last digit10let i = 0; // binary number position11let acc = 0;1213while (j >= 0) {14let positionNumber = Number(s[j]) * Math.pow(2, i);15j--;16i++;17if (acc + positionNumber > k) continue;1819acc += positionNumber;20count++;21}2223return count;24};