java.lang.Object
g2401_2500.s2438_range_product_queries_of_powers.Solution

public class Solution extends Object
2438 - Range Product Queries of Powers.<p>Medium</p> <p>Given a positive integer <code>n</code>, there exists a <strong>0-indexed</strong> array called <code>powers</code>, composed of the <strong>minimum</strong> number of powers of <code>2</code> that sum to <code>n</code>. The array is sorted in <strong>non-decreasing</strong> order, and there is <strong>only one</strong> way to form the array.</p> <p>You are also given a <strong>0-indexed</strong> 2D integer array <code>queries</code>, where <code>queries[i] = [left<sub>i</sub>, right<sub>i</sub>]</code>. Each <code>queries[i]</code> represents a query where you have to find the product of all <code>powers[j]</code> with <code>left<sub>i</sub> <= j <= right<sub>i</sub></code>.</p> <p>Return <em>an array</em> <code>answers</code><em>, equal in length to</em> <code>queries</code><em>, where</em> <code>answers[i]</code> <em>is the answer to the</em> <code>i<sup>th</sup></code> <em>query</em>. Since the answer to the <code>i<sup>th</sup></code> query may be too large, each <code>answers[i]</code> should be returned <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 15, queries = [[0,1],[2,2],[0,3]]</p> <p><strong>Output:</strong> [2,4,64]</p> <p><strong>Explanation:</strong></p> <p>For n = 15, powers = [1,2,4,8]. It can be shown that powers cannot be a smaller size.</p> <p>Answer to 1st query: powers[0] * powers[1] = 1 * 2 = 2.</p> <p>Answer to 2nd query: powers[2] = 4.</p> <p>Answer to 3rd query: powers[0] * powers[1] * powers[2] * powers[3] = 1 * 2 * 4 * 8 = 64.</p> <p>Each answer modulo 10<sup>9</sup> + 7 yields the same answer, so [2,4,64] is returned.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 2, queries = [[0,0]]</p> <p><strong>Output:</strong> [2]</p> <p><strong>Explanation:</strong> For n = 2, powers = [2]. The answer to the only query is powers[0] = 2. The answer modulo 10<sup>9</sup> + 7 is the same, so [2] is returned.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 10<sup>9</sup></code></li> <li><code>1 <= queries.length <= 10<sup>5</sup></code></li> <li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> < powers.length</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • productQueries

      public int[] productQueries(int n, int[][] queries)