You are given an integer array nums.
For any positive integer x, define the following sequence:
p0 = xpi+1 = popcount(pi) for all i >= 0, where popcount(y) is the number of set bits (1's) in the binary representation of y.This sequence will eventually reach the value 1.
The popcount-depth of x is defined as the smallest integer d >= 0 such that pd = 1.
For example, if x = 7 (binary representation "111"). Then, the sequence is: 7 → 3 → 2 → 1, so the popcount-depth of 7 is 3.
You are also given a 2D integer array queries, where each queries[i] is either:
[1, l, r, k] - Determine the number of indices j such that l <= j <= r and the popcount-depth of nums[j] is equal to k.[2, idx, val] - Update nums[idx] to val.Return an integer array answer, where answer[i] is the number of indices for the ith query of type [1, l, r, k].
Example 1:
Input: nums = [2,4], queries = [[1,0,1,1],[2,1,1],[1,0,1,0]]
Output: [2,1]
Explanation:
i |
queries[i] |
nums |
binary(nums) |
popcount- depth |
[l, r] |
k |
Validnums[j] |
updatednums |
Answer |
|---|---|---|---|---|---|---|---|---|---|
| 0 | [1,0,1,1] | [2,4] | [10, 100] | [1, 1] | [0, 1] | 1 | [0, 1] | — | 2 |
| 1 | [2,1,1] | [2,4] | [10, 100] | [1, 1] | — | — | — | [2,1] | — |
| 2 | [1,0,1,0] | [2,1] | [10, 1] | [1, 0] | [0, 1] | 0 | [1] | — | 1 |
Thus, the final answer is [2, 1].
Example 2:
Input: nums = [3,5,6], queries = [[1,0,2,2],[2,1,4],[1,1,2,1],[1,0,1,0]]
Output: [3,1,0]
Explanation:
i |
queries[i] |
nums |
binary(nums) |
popcount- depth |
[l, r] |
k |
Validnums[j] |
updatednums |
Answer |
|---|---|---|---|---|---|---|---|---|---|
| 0 | [1,0,2,2] | [3, 5, 6] | [11, 101, 110] | [2, 2, 2] | [0, 2] | 2 | [0, 1, 2] | — | 3 |
| 1 | [2,1,4] | [3, 5, 6] | [11, 101, 110] | [2, 2, 2] | — | — | — | [3, 4, 6] | — |
| 2 | [1,1,2,1] | [3, 4, 6] | [11, 100, 110] | [2, 1, 2] | [1, 2] | 1 | [1] | — | 1 |
| 3 | [1,0,1,0] | [3, 4, 6] | [11, 100, 110] | [2, 1, 2] | [0, 1] | 0 | [] | — | 0 |
Thus, the final answer is [3, 1, 0].
Example 3:
Input: nums = [1,2], queries = [[1,0,1,1],[2,0,3],[1,0,0,1],[1,0,0,2]]
Output: [1,0,1]
Explanation:
i |
queries[i] |
nums |
binary(nums) |
popcount- depth |
[l, r] |
k |
Validnums[j] |
updatednums |
Answer |
|---|---|---|---|---|---|---|---|---|---|
| 0 | [1,0,1,1] | [1, 2] | [1, 10] | [0, 1] | [0, 1] | 1 | [1] | — | 1 |
| 1 | [2,0,3] | [1, 2] | [1, 10] | [0, 1] | — | — | — | [3, 2] | |
| 2 | [1,0,0,1] | [3, 2] | [11, 10] | [2, 1] | [0, 0] | 1 | [] | — | 0 |
| 3 | [1,0,0,2] | [3, 2] | [11, 10] | [2, 1] | [0, 0] | 2 | [0] | — | 1 |
Thus, the final answer is [1, 0, 1].
Constraints:
1 <= n == nums.length <= 1051 <= nums[i] <= 10151 <= queries.length <= 105queries[i].length == 3 or 4
queries[i] == [1, l, r, k] or,queries[i] == [2, idx, val]0 <= l <= r <= n - 10 <= k <= 50 <= idx <= n - 11 <= val <= 1015Problem Overview: You are given integers and need to count how many have a popcount-depth equal to K. The popcount-depth of a number is defined by repeatedly applying the bit count operation (popcount) until the value becomes 1. The challenge is efficiently tracking how many numbers satisfy this property while processing large input sizes.
Approach 1: Brute Force Popcount Simulation (O(n log V) time, O(1) space)
The most direct solution computes the popcount-depth for every integer independently. For each number, repeatedly apply popcount until the value becomes 1 and count how many steps were needed. Compare that depth with K and increment the result if it matches. Each popcount reduces the number size quickly, so the number of iterations is bounded by O(log V), where V is the maximum integer value. This approach is easy to implement but becomes slow if the problem involves repeated queries or updates.
Approach 2: Precomputation + Frequency Counting (O(n log V) time, O(n) space)
A better strategy precomputes the popcount-depth for all possible bit counts that can appear. For any number x, compute bits = popcount(x), then repeatedly apply popcount to determine the depth. Since the maximum bit count for standard integers is small (≤ 64), you can cache depths for each bit count. While iterating through the array, compute popcount(x), look up its depth, and update a frequency structure. This avoids repeated recomputation and speeds up processing significantly.
Approach 3: Divide and Conquer with Fenwick/Segment Tree (O(n log n) time, O(n) space)
When the problem includes dynamic queries or range-based counting, a tree-based structure becomes necessary. After mapping each number to its popcount-depth, maintain counts using a Fenwick Tree (Binary Indexed Tree) or Segment Tree. Each node stores how many numbers currently have a given depth. Updates modify the corresponding index, and queries aggregate counts efficiently in O(log n) time. This technique combines bit manipulation with classic range-query structures from Binary Indexed Tree, Segment Tree, and Divide and Conquer patterns.
Recommended for interviews: Start with the brute force explanation to show you understand the definition of popcount-depth. Then move to the optimized solution that precomputes depths and maintains counts using a Fenwick Tree or Segment Tree. Interviewers typically expect the O(n log n) approach because it demonstrates familiarity with range-query data structures and efficient counting strategies.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Popcount Simulation | O(n log V) | O(1) | Small input sizes or when only a single pass is required |
| Precomputation + Frequency Counting | O(n log V) | O(n) | When popcount-depth values repeat and can be cached |
| Fenwick Tree / Segment Tree | O(n log n) | O(n) | Large datasets with updates or range queries requiring fast aggregation |
3624. Number of Integers With Popcount-Depth Equal to K II | Segment Tree • Amit Dhyani • 671 views views
Watch 3 more video solutions →Practice Number of Integers With Popcount-Depth Equal to K II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor