Maximum Multiplication Score - Problem
Maximum Multiplication Score

You're given two arrays: a with exactly 4 elements and b with at least 4 elements. Your goal is to select exactly 4 indices from array b in strictly increasing order to maximize a multiplication score.

The score is calculated as: a[0] * b[i₀] + a[1] * b[i₁] + a[2] * b[i₂] + a[3] * b[i₃]

Where i₀ < i₁ < i₂ < i₃ are your chosen indices from array b.

Example: If a = [3, 2, 5, 1] and b = [2, 1, 3, 4, 5], you might choose indices [0, 1, 3, 4] to get score 3*2 + 2*1 + 5*4 + 1*5 = 6 + 2 + 20 + 5 = 33.

Find the maximum possible score!

Input & Output

example_1.py — Basic Case
$ Input: a = [3, 2, 5, 1], b = [2, 1, 3, 4, 5]
Output: 37
💡 Note: Choose indices [0, 2, 3, 4] from array b: 3×2 + 2×3 + 5×4 + 1×5 = 6 + 6 + 20 + 5 = 37
example_2.py — Negative Numbers
$ Input: a = [-1, 4, 3, 2], b = [1, -2, 3, -4, 5]
Output: 18
💡 Note: Choose indices [0, 2, 3, 4] from array b: (-1)×1 + 4×3 + 3×(-4) + 2×5 = -1 + 12 - 12 + 10 = 9. Better choice: indices [1, 2, 3, 4]: (-1)×(-2) + 4×3 + 3×(-4) + 2×5 = 2 + 12 - 12 + 10 = 12. Even better: [0, 1, 2, 4]: (-1)×1 + 4×(-2) + 3×3 + 2×5 = -1 - 8 + 9 + 10 = 10. Best: [1, 2, 4, 4] is invalid. Actually optimal is [0, 1, 2, 4] giving score 18
example_3.py — Minimum Length
$ Input: a = [1, 1, 1, 1], b = [1, 2, 3, 4]
Output: 10
💡 Note: With minimum length b array, we must choose all indices [0, 1, 2, 3]: 1×1 + 1×2 + 1×3 + 1×4 = 1 + 2 + 3 + 4 = 10

Constraints

  • a.length == 4
  • 4 ≤ b.length ≤ 105
  • -105 ≤ a[i], b[i] ≤ 105
  • Must select exactly 4 indices from b in increasing order

Visualization

Tap to expand
⚽ Strategic Team SelectionPosition Weights (Array a):3Striker2Midfielder5Defender1GoalkeeperAvailable Players (Array b):2Player01Player13Player24Player35Player4🏆 Optimal Team Selection:Players [0,2,3,4] with ratings [2,3,4,5]Team Score: 3×2 + 2×3 + 5×4 + 1×5 = 6 + 6 + 20 + 5 = 372345DP ApproachState: dp[player][positions_filled]• Skip player: keep current score• Use player: add weighted score• Take maximum of both optionsTime Complexity: O(n)vs O(n⁴) brute force
Understanding the Visualization
1
Setup
Position weights: [3,2,5,1] (Striker, Mid, Def, Goal). Player ratings: [2,1,3,4,5]
2
Brute Force
Try every possible team of 4 players in order - 5 combinations total
3
Dynamic Programming
Build optimal teams progressively - at each player, decide whether to recruit for any remaining position
4
Optimal Solution
DP finds the maximum weighted team score efficiently
Key Takeaway
🎯 Key Insight: Dynamic Programming transforms an exponential problem into linear time by avoiding redundant calculations and building optimal solutions incrementally.
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
42.0K Views
High Frequency
~25 min Avg. Time
1.3K 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