Find Products of Elements of Big Array - Problem
Find Products of Elements of Big Array
Imagine a magical array called
For example:
โข Number
โข Number
The
Your task: Given multiple queries
Can you solve this efficiently without actually building the massive 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
Understanding the Visualization
1
Extract Powers
Each number contributes its binary powers to the sequence
2
Mathematical Mapping
Use formulas to find position without building array
3
Efficient Calculation
Count and multiply using modular arithmetic
Key Takeaway
๐ฏ Key Insight: Instead of building the massive array, use mathematical formulas to count power occurrences and binary search for efficient index mapping!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code