Count K-Reducible Numbers Less Than N - Problem
You are given a binary string s representing a number n in its binary form. You are also given an integer k.
An integer x is called k-reducible if performing the following operation at most k times reduces it to 1:
- Replace
xwith the count of set bits in its binary representation.
For example, the binary representation of 6 is "110". Applying the operation once reduces it to 2 (since "110" has two set bits). Applying the operation again to 2 (binary "10") reduces it to 1 (since "10" has one set bit).
Return an integer denoting the number of positive integers less than n that are k-reducible. Since the answer may be too large, return it modulo 109 + 7.
Input & Output
Example 1 — Basic Case
$
Input:
s = "111", k = 1
›
Output:
3
💡 Note:
n = 7 in decimal. Check numbers 1-6: 1 (already 1, 0 steps ≤ 1), 2 (binary "10", 1 bit → 1, 1 step ≤ 1), 3 (binary "11", 2 bits → 2 → 1, 2 steps > 1), 4 (binary "100", 1 bit → 1, 1 step ≤ 1), 5 (binary "101", 2 bits → 2 → 1, 2 steps > 1), 6 (binary "110", 2 bits → 2 → 1, 2 steps > 1). Numbers 1, 2, and 4 are 1-reducible. Answer is 3.
Example 2 — Larger k
$
Input:
s = "1000", k = 2
›
Output:
6
💡 Note:
n = 8 in decimal. Check 1-7: All numbers 1-7 can be reduced to 1 in at most 2 steps, except maybe some. Most small numbers are 2-reducible, so answer is 6.
Example 3 — Small Case
$
Input:
s = "11", k = 1
›
Output:
2
💡 Note:
n = 3 in decimal. Check 1,2: 1 needs 0 steps, 2 needs 1 step (2→1). Both are 1-reducible.
Constraints
- 1 ≤ s.length ≤ 105
- s consists of only '0' and '1'
- s does not have leading zeros
- 1 ≤ k ≤ 5
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code