Product in Power Ranges

Efficient solution for range product queries of powers: binary decomposition of n into powers of 2, use of exponent prefixes and modular exponentiation (mod 1e9+7) for fast answers.

domingo, 17 de agosto de 2025 • 4 min read • Q2BSTUDIO Team

Artificial-Intelligence-

2438. Range Product Queries of Powers

Problem description: Given a positive integer n, an array powers indexed from 0 is constructed, formed by the minimum number of powers of 2 that sum to n. The array powers is sorted in non-decreasing order and there is a unique representation. A 2D array queries is received, where each queries[i] = [lefti, righti] represents a query asking for the product of all elements powers[j] with lefti <= j <= righti. Return an array answers with the answers to each query taken modulo 10^9+7.

Example 1: Input n = 15, queries = [[0,1],[2,2],[0,3]] Output [2,4,64] Explanation: for n = 15 powers = [1,2,4,8]. Product of indices 0 to 1 = 1*2 = 2. Index 2 = 4. Product from 0 to 3 = 1*2*4*8 = 64.

Example 2: Input n = 2, queries = [[0,0]] Output [2] Explanation: for n = 2 powers = [2]. The only query returns 2.

Main idea: The decomposition of n into powers of 2 is obtained directly from its binary representation. Each bit set to 1 at position i corresponds to the power 2^i. The array powers contains these powers in non-decreasing order if we traverse the bits from least significant to most significant.

Efficient solution: Instead of multiplying range by range, we take advantage of the fact that each element is a power of 2. If we store for each position its exponent i, the product of a range corresponds to 2 raised to the sum of those exponents. Steps: 1 Build the list exps with the exponents i such that bit i of n is 1. 2 Calculate a prefix pref of exponent sums where pref[0] = 0 and pref[k+1] = pref[k] + exps[k]. 3 For a query [l,r] calculate sumExp = pref[r+1] - pref[l]. 4 Calculate powmod(2,sumExp,MOD) with MOD = 10^9+7 to obtain the answer. This approach avoids repeated multiplications and is very fast because the size of powers is at most 31 for n <= 10^9.

Complexity: Array construction in O(log n). Preprocessing of pref in O(m) with m number of bits set to 1. Each query is answered in O(log MOD) time due to modular exponentiation, or in O(1) time if optimized binary exponentiation is used. Additional space O(m).

Key implementation points: correctly handle 0-based indices, use modular exponentiation to avoid overflow, and traverse the bits of n from bit 0 to the most significant to obtain sorted powers. Simple alternative: you can also store the array powers with values 1,2,4,... and a prefix of products modulo MOD to answer queries in O(1) per query using modular inverses, although the exponent strategy is usually simpler and numerically stable.

Constraints: 1 <= n <= 10^9, 1 <= queries.length <= 10^5, 0 <= starti <= endi < powers.length.

Hint: The binary representation of n directly generates the array powers and the sum of exponents in the range determines the resulting power of 2.

Conceptual implementation: 1 Create empty vector exps. 2 Traverse i from 0 until n is 0: if n & 1 is 1 add i to exps; n >>= 1; i++. 3 Build pref of accumulated sizes. 4 For each query calculate sumExp and respond with powmod(2,sumExp,MOD).

About Q2BSTUDIO: Q2BSTUDIO is a custom software development company specialized in custom applications and custom software for businesses of all sizes. We offer artificial intelligence solutions and AI for businesses, development of custom AI agents, Power BI integration and business intelligence services to transform data into value. We are also experts in cybersecurity to protect critical infrastructures and in aws and azure cloud services to deploy scalable and secure applications. Our services include consulting, implementation and ongoing support for projects that require artificial intelligence, advanced analytics, AI agents and cloud solutions. We work with agile methodologies to deliver custom applications that fit real business needs.

Why choose us: experience in applied artificial intelligence, custom software projects with a focus on security and performance, migrations and architectures in aws and azure cloud services, and creation of dashboards with Power BI for data-driven decisions. If you are looking for partners in cybersecurity, business intelligence, AI agents or custom application development, Q2BSTUDIO offers a multidisciplinary team capable of taking the project from idea to production.

Keywords for positioning: custom applications, custom software, artificial intelligence, cybersecurity, aws and azure cloud services, business intelligence services, AI for businesses, AI agents, power bi.

Contact and next steps: get in touch with Q2BSTUDIO to evaluate your custom software or artificial intelligence project. We can help design the architecture, select appropriate cloud services, implement cybersecurity measures and create business intelligence solutions with Power BI that drive your company's growth.

A BREAK?

Play for a moment before you go

OUR SERVICES

How we can help you

Do you have a project in mind?

Tell us your vision and we'll turn it into a software solution. Whatever the scope, we make your idea real.