LeetCode: Reordered Power Of 2 Solution

1/**
2 * @param {number} N
3 * @return {boolean}
4 */
5var reorderedPowerOf2 = function (N) {
6 const getDigits = number => {
7 const digits = Array(10).fill(0)
8 while (number) {
9 digits[number % 10]++
10 number = Math.floor(number / 10)
11 }
12 return digits.join("")
13 }
14
15 // 0 <= N <= 1e9
16 // 0 <= 2^NUMBER_OF_DIGITS <= 1e9
17 // 0 <= NUMBER_OF_DIGITS <= 30
18 const MAX_NUMBER_OF_DIGITS = Math.ceil(Math.log2(1e9))
19
20 for (let exp = 0; exp <= MAX_NUMBER_OF_DIGITS; exp++) {
21 if (getDigits(N) === getDigits(Math.pow(2, exp))) {
22 return true
23 }
24 }
25 return false
26}

Comments

Loading comments...

Tags

leetcode

math

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Vowel Spellchecker

HoningJS

Search Posts