Find Products of Elements of Big Array - Problem

Find Products of Elements of Big Array

Imagine a magical array called big_nums that's constructed in a fascinating way! For every positive integer, we create its powerful array - which contains all the powers of 2 that sum up to that number (based on its binary representation).

For example:
• Number 13 has binary 1101, so its powerful array is [1, 4, 8] (positions of 1-bits)
• Number 23 has binary 10111, so its powerful array is [1, 2, 4, 16]

The big_nums array is formed by concatenating all these powerful arrays in order:
[1, 2, 1, 2, 4, 1, 4, 2, 4, 1, 2, 4, 8, ...]

Your task: Given multiple queries [from, to, mod], calculate the product of elements from index from to to (inclusive) in big_nums, modulo mod.

Can you solve this efficiently without actually building the massive array?

Input & Output

example_1.py — Basic Query
$ Input: queries = [[0, 2, 3]]
Output: [2]
💡 Note: big_nums = [1, 2, 1, 2, 4, ...]. Range [0,2] contains [1,2,1]. Product = 1×2×1 = 2. Since 2 < 3, result is 2.
example_2.py — Multiple Queries
$ Input: queries = [[2, 5, 4], [0, 2, 3]]
Output: [0, 2]
💡 Note: Range [2,5] = [1,2,4,1] → product = 8 ≡ 0 (mod 4). Range [0,2] = [1,2,1] → product = 2.
example_3.py — Large Numbers
$ Input: queries = [[10, 15, 7]]
Output: [1]
💡 Note: For large ranges, we need efficient calculation without building the actual array. The product modulo 7 equals 1.

Constraints

  • 1 ≤ queries.length ≤ 500
  • queries[i].length == 3
  • 0 ≤ fromi ≤ toi ≤ 1015
  • 2 ≤ modi ≤ 109
  • The answer is guaranteed to fit in a 32-bit integer.

Visualization

Tap to expand
Find Products of Elements of Big Array INPUT Building big_nums from binary: 1 = 1 --> [1] 2 = 10 --> [2] 3 = 11 --> [1,2] 4 = 100 --> [4] big_nums array: 1 0 2 1 1 2 2 3 4 4 1 5 ... Query: [0, 2] Query: [[0, 2, 3]] from=0, to=2, mod=3 ALGORITHM STEPS 1 Count Elements Binary search to find which numbers contribute to range 2 Sum Exponents Count total power of 2 for each bit position in range 3 Modular Power Compute 2^sum mod m using fast exponentiation 4 Combine Results Multiply all powers mod m Calculation for [0,2]: Elements: 1, 2, 1 = 2^0, 2^1, 2^0 Product = 2^(0+1+0) = 2^1 = 2 mod 3 = 2 Result: 2 FINAL RESULT Query Result: [2] Verification: big_nums[0:2] = [1,2,1] 1 * 2 * 1 = 2 2 mod 3 = 2 OK Output: [2] Key Insight: Instead of building big_nums, use digit DP to count how many times each power of 2 appears in range. The product of 2^a * 2^b * 2^c = 2^(a+b+c). Sum exponents per bit position, then use modular exponentiation. Time complexity: O(log^2(N) * Q) where N is max index and Q is number of queries. TutorialsPoint - Find Products of Elements of Big Array | Optimal Solution
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 22
42.0K Views
Medium Frequency
~35 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen