Ctrl K

Products of Binary Pieces

Medium
A positive number total can be written in exactly one way as a sum of distinct powers of two (that is its binary form). List those powers in increasing order to get the array pieces, indexed from 0. You are given queries queries, where queries[i] = [l, r]. The answer to a query is the product pieces[l] · pieces[l+1] · ... · pieces[r]. It can be huge, so return it modulo 10⁹ + 7. Return the answers to all the queries, in order.
Example 1
total=13, queries=[[0, 1], [1, 2], [2, 2]] → [4, 32, 8]

13 = 1 + 4 + 8, so the pieces are [1, 4, 8]. The products are 1·4 = 4, 4·8 = 32 and just 8.

Example 2
total=6, queries=[[0, 1]] → [8]

6 = 2 + 4, the pieces are [2, 4], and their product is 8.

Constraints
  • 1 ≤ total ≤ 10⁹
  • 1 ≤ queries.length ≤ 10⁵
  • 0 ≤ l ≤ r < pieces.length
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPalindrome After a Few Fixes MediumRange XOR Lookups MediumSplit Into Equal-XOR Halves MediumK-th Largest Corner XOR MediumXOR Top-Up While Trimming Medium
loading Python…
Input
total=13, queries=[[0, 1], [1, 2], [2, 2]]
Expected
[4, 32, 8]

Run checks the first 3 tests in your browser. Submit checks all 55 tests on the server, hidden ones included.