1365. Count Integers in Range with Popcount Condition

MediumBit ManipulationBit ManipulationCombinatoricsDigit DP

Given a range [lo, hi] and an integer k, count how many integers in that inclusive range have exactly k set bits in their binary representation.

Input: A JSON object {"lo": <range start>, "hi": <range end>, "k": <target popcount>} with lo <= hi.

Output: Return the count of integers in [lo, hi] whose popcount equals k.

Examples

Example 1
Input: {"lo":0,"hi":7,"k":2}
Output: 3
Explanation: 3, 5 and 6 each have two set bits -> 3.
Example 2
Input: {"lo":1,"hi":10,"k":1}
Output: 4
Explanation: 1, 2, 4 and 8 are the powers of two in range -> 4.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →