Class Solution
- java.lang.Object
-
- g2401_2500.s2438_range_product_queries_of_powers.Solution
-
public class Solution extends Object
2438 - Range Product Queries of Powers.Medium
Given a positive integer
n, there exists a 0-indexed array calledpowers, composed of the minimum number of powers of2that sum ton. The array is sorted in non-decreasing order, and there is only one way to form the array.You are also given a 0-indexed 2D integer array
queries, wherequeries[i] = [lefti, righti]. Eachqueries[i]represents a query where you have to find the product of allpowers[j]withlefti <= j <= righti.Return an array
answers, equal in length toqueries, whereanswers[i]is the answer to theithquery. Since the answer to theithquery may be too large, eachanswers[i]should be returned modulo109 + 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]. It can be shown that powers cannot be a smaller size.
Answer to 1st query: powers[0] * powers[1] = 1 * 2 = 2.
Answer to 2nd query: powers[2] = 4.
Answer to 3rd query: powers[0] * powers[1] * powers[2] * powers[3] = 1 * 2 * 4 * 8 = 64.
Each answer modulo 109 + 7 yields the same answer, so [2,4,64] is returned.
Example 2:
Input: n = 2, queries = [[0,0]]
Output: [2]
Explanation: For n = 2, powers = [2]. The answer to the only query is powers[0] = 2. The answer modulo 109 + 7 is the same, so [2] is returned.
Constraints:
1 <= n <= 1091 <= queries.length <= 1050 <= starti <= endi < powers.length
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]productQueries(int n, int[][] queries)
-