Skip to content

2438. Range Product Queries of Powers #2039

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to efficiently answer multiple range product queries on an array derived from the binary representation of a given positive integer n. The array consists of the minimum number of distinct powers of 2 that sum to n, sorted in non-decreasing order. Each query requires computing the product of elements within a specified range in this array, modulo 109 + 7.

Approach

  1. Generate Powers Array:

    • The powers array is constructed by decomposing n into its binary representation. Each set bit in the binary form of n corresponds to a power of 2. For example, if n is 15 (binary 1111), the powers array will be [1, 2, 4, 8].
    • We iterate through each bit of n from the least significant bit (LSB) to…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Aug 11, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants