Find Sum of Array Product of Magical Sequences - Problem

๐Ÿ”ฎ Find Sum of Array Product of Magical Sequences

You're given an array nums and two integers m and k. Your task is to find all magical sequences and calculate the sum of their array products.

A sequence seq of m indices is considered magical if:

  • It contains exactly m elements
  • Each element seq[i] is a valid index: 0 โ‰ค seq[i] < nums.length
  • The binary representation of 2^seq[0] + 2^seq[1] + ... + 2^seq[m-1] has exactly k set bits (1s)

The array product of a sequence is: nums[seq[0]] ร— nums[seq[1]] ร— ... ร— nums[seq[m-1]]

Example: If seq = [0, 2] and nums = [3, 1, 4], then the array product is nums[0] ร— nums[2] = 3 ร— 4 = 12.

Return the sum of array products for all valid magical sequences modulo 10^9 + 7.

Input & Output

example_1.py โ€” Basic case
$ Input: nums = [2, 3, 4], m = 2, k = 2
โ€บ Output: 34
๐Ÿ’ก Note: Magical sequences: [0,1] (2^0+2^1=3, bits=2, product=2ร—3=6), [0,2] (2^0+2^2=5, bits=2, product=2ร—4=8), [1,2] (2^1+2^2=6, bits=2, product=3ร—4=12). Sum = 6+8+12 = 26. Wait, let me recalculate: For [0,1]: mask=0b11, bits=2, product=6. For [0,2]: mask=0b101, bits=2, product=8. For [1,2]: mask=0b110, bits=2, product=12. Total=26.
example_2.py โ€” Single element
$ Input: nums = [1, 2, 3], m = 1, k = 1
โ€บ Output: 6
๐Ÿ’ก Note: For m=1, k=1, we need sequences of length 1 where 2^seq[0] has exactly 1 set bit. This is true for any single index. Sequences: [0] (product=1), [1] (product=2), [2] (product=3). Sum = 1+2+3 = 6.
example_3.py โ€” No valid sequences
$ Input: nums = [1, 1], m = 2, k = 1
โ€บ Output: 0
๐Ÿ’ก Note: For m=2, k=1, we need sequences of length 2 where the binary sum has exactly 1 set bit. The only sequence [0,1] gives 2^0+2^1=3, which has 2 set bits, not 1. Therefore, no valid magical sequences exist.

Visualization

Tap to expand
๐Ÿ”ฎ Magical Sequences ProblemStep 1: Choose m=2 magical itemsArray: [2, 3, 4] โ†’ Select indices [0, 1] with values [2, 3]Step 2: Calculate magical powerPower = 2^0 + 2^1 = 1 + 2 = 3 (binary: 011)Step 3: Count energy sparks (set bits)Binary 011 has 2 set bits โœ“ (matches k=2)Step 4: Calculate contributionProduct = nums[0] ร— nums[1] = 2 ร— 3 = 62Item 03Item 14Item 2โœจ Magic happens when:โ€ข Choose exactly m itemsโ€ข Power sum has k set bits
Understanding the Visualization
1
Choose Items
Select exactly m items from the magical collection
2
Calculate Power
Sum the powers: 2^pos1 + 2^pos2 + ... + 2^posm
3
Count Energy Sparks
Count set bits in the binary representation of the power sum
4
Validate Magic
If sparks equal k, multiply item values and add to total
Key Takeaway
๐ŸŽฏ Key Insight: Use dynamic programming to efficiently explore all combinations while tracking both the number of selected elements and set bits, avoiding the exponential time complexity of brute force.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n^m * m)

n^m combinations, each requiring m operations to calculate product and bit counting

n
2n
โœ“ Linear Growth
Space Complexity
O(m)

Space for storing current combination of size m

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 20
  • 1 โ‰ค m โ‰ค nums.length
  • 1 โ‰ค k โ‰ค nums.length
  • 1 โ‰ค nums[i] โ‰ค 109
  • The array product can be very large, hence return modulo 109 + 7
Asked in
Google 25 Amazon 18 Meta 15 Microsoft 12
26.2K Views
Medium Frequency
~35 min Avg. Time
847 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