DDSA Solutions

762. Prime Number of Set Bits in Binary Representation

Problem Overview

Prime Number of Set Bits in Binary Representation (Medium) asks you to solve a structured algorithmic task. This is a common Math / Bit Manipulation pattern in coding interviews. Bit manipulation with prime bitmask. Precompute a magic integer with

A full step-by-step explanation is being added. See the study guide for pattern-based practice.

Approach

Bit manipulation with prime bitmask. Precompute a magic integer with

bits set at prime positions (2,3,5,7,11,13,17,19). For each number in [left,right],

count set bits and check if that count's bit is set in the magic mask.

Related patterns: Math, Bit Manipulation

762.cs
C#
// Approach: Bit manipulation with prime bitmask. Precompute a magic integer with
// bits set at prime positions (2,3,5,7,11,13,17,19). For each number in [left,right],
// count set bits and check if that count's bit is set in the magic mask.
// Time: O((right-left) * log(right)) Space: O(1)
public class Solution
{
    public int CountPrimeSetBits(int left, int right)
    {
        // {2, 3, 5, 7, 11, 13, 17, 19}-th bits are 1s.
        // 0b10100010100010101100 = 665772
        const int magic = 665772;
        int ans = 0;

        for (int num = left; num <= right; ++num)
        {
            if (((magic >> CountBits(num)) & 1) == 1)
                ++ans;
        }

        return ans;
    }

    private int CountBits(int n)
    {
        int count = 0;
        while (n > 0)
        {
            count += n & 1;
            n >>= 1;
        }
        return count;
    }
}
Was this solution helpful?

Related Problems