Find Sum of Array Product of Magical Sequences - Problem

You are given two integers m and k, and an integer array nums.

A sequence of integers seq is called magical if:

  • seq has a size of m
  • 0 <= seq[i] < nums.length
  • The binary representation of 2^seq[0] + 2^seq[1] + ... + 2^seq[m-1] has k set bits

The array product of this sequence is defined as prod(seq) = (nums[seq[0]] * nums[seq[1]] * ... * nums[seq[m-1]]).

Return the sum of the array products for all valid magical sequences. Since the answer may be large, return it modulo 10^9 + 7.

A set bit refers to a bit in the binary representation of a number that has a value of 1.

Input & Output

Example 1 — Basic Case
$ Input: nums = [2,3,1], m = 2, k = 2
Output: 22
💡 Note: Valid magical sequences: [0,0] (2^0+2^0=2, 1 set bit, but k=2 so invalid), [0,1] (2^0+2^1=3, 2 set bits, product=2*3=6), [0,2] (2^0+2^2=5, 2 set bits, product=2*1=2), [1,0] (2^1+2^0=3, 2 set bits, product=3*2=6), [1,2] (2^1+2^2=6, 2 set bits, product=3*1=3), [2,0] (2^2+2^0=5, 2 set bits, product=1*2=2), [2,1] (2^2+2^1=6, 2 set bits, product=1*3=3). Sum = 6+2+6+3+2+3 = 22.
Example 2 — Single Element
$ Input: nums = [5], m = 1, k = 1
Output: 5
💡 Note: Only sequence [0] is valid: 2^0=1 has 1 set bit, product=5.
Example 3 — No Valid Sequences
$ Input: nums = [1,2], m = 2, k = 1
Output: 5
💡 Note: Possible sequences of length 2: [0,0] (2^0+2^0=2, 1 set bit, product=1*1=1), [0,1] (2^0+2^1=3, 2 set bits), [1,0] (2^1+2^0=3, 2 set bits), [1,1] (2^1+2^1=4, 1 set bit, product=2*2=4). Valid sequences with k=1 set bits: [0,0] and [1,1]. Sum = 1+4 = 5.

Constraints

  • 1 ≤ nums.length ≤ 20
  • 1 ≤ nums[i] ≤ 109
  • 1 ≤ m ≤ nums.length
  • 1 ≤ k ≤ m

Visualization

Tap to expand
Find Sum of Array Product of Magical Sequences INPUT nums array: 2 idx: 0 3 idx: 1 1 idx: 2 Parameters: m = 2 k = 2 (seq size=2, set bits=2) Magical Sequences: [0,1]: 2^0+2^1=3 (11) [0,2]: 2^0+2^2=5 (101) [1,0]: 2^1+2^0=3 (11) [1,2]: 2^1+2^2=6 (110) [2,0]: 2^2+2^0=5 (101) [2,1]: 2^2+2^1=6 (110) ALGORITHM STEPS 1 DP State Setup dp[pos][bitMask][setBits] 2 Iterate Positions For each pos in seq[0..m-1] 3 Track Set Bits Update mask: mask | 2^seq[i] 4 Accumulate Products Sum when popcount(mask)=k Product Calculations: [0,1]: 2*3 = 6 [1,0]: 3*2 = 6 (same mask) [0,2]: 2*1 = 2 [2,0]: 1*2 = 2 (same mask) [1,2]: 3*1 = 3 [2,1]: 1*3 = 3 (same mask) FINAL RESULT Sum of All Products: Seq [0,1]: prod = 6 Seq [1,0]: (counted above) Seq [0,2]: prod = 2 Seq [2,0]: (counted above) Seq [1,2]: prod = 3 Seq [2,1]: (counted above) Unique: 6 + 2 + 3 = 11 Note: Order matters! Distinct seqs give same bitmask: count once per k OUTPUT 9 Key Insight: Use DP with bitmask to track which bits are set in 2^seq[0] + 2^seq[1] + ... + 2^seq[m-1]. The key is that popcount(mask) must equal k. Multiply nums values and accumulate products for valid sequences. Use modulo 10^9+7 to prevent overflow. Time: O(m * 2^n * n). TutorialsPoint - Find Sum of Array Product of Magical Sequences | Optimized DP with Bit Manipulation
Asked in
Google 25 Microsoft 18 Meta 15 Amazon 12
12.0K Views
Medium Frequency
~35 min Avg. Time
450 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