Prime Number of Set Bits in Binary Representation

Given two integers left and right, return the count of numbers in the inclusive range [left, right] having a prime number of set bits in their binary representation.

Recall that the number of set bits an integer has is the number of 1's present when written in binary.

  • For example, 21 written in binary is 10101, which has 3 set bits.
Example 1
Inputleft = 6, right = 10
Output4
The numbers 6, 7, 9, and 10 have a prime number of set bits, so the count is 4.
Example 2
Inputleft = 10, right = 15
Output5
The numbers 10, 11, 12, 13, and 14 have a prime number of set bits, so the count is 5.

Constraints

  • 1 <= left <= right <= 10^6
  • 0 <= right - left <= 10^4

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate