Maximum Multiplication Score - Problem

You are given an integer array a of size 4 and another integer array b of size at least 4.

You need to choose 4 indices i₀, i₁, i₂, i₃ from array b such that i₀ < i₁ < i₂ < i₃.

Your score will be equal to a[0] * b[i₀] + a[1] * b[i₁] + a[2] * b[i₂] + a[3] * b[i₃].

Return the maximum score you can achieve.

Input & Output

Example 1 — Basic Case
$ Input: a = [3,2,-1,1], b = [5,-3,4,2,-1,6]
Output: 30
💡 Note: Choose indices [0,2,4,5]: 3×5 + 2×4 + (-1)×(-1) + 1×6 = 15 + 8 + 1 + 6 = 30
Example 2 — Negative Values
$ Input: a = [-1,4,5,2], b = [2,1,8,3,6]
Output: 58
💡 Note: Choose indices [1,2,3,4]: (-1)×1 + 4×8 + 5×3 + 2×6 = -1 + 32 + 15 + 12 = 58
Example 3 — Minimum Size
$ Input: a = [1,2,3,4], b = [5,6,7,8]
Output: 70
💡 Note: Must use all indices [0,1,2,3]: 1×5 + 2×6 + 3×7 + 4×8 = 5 + 12 + 21 + 32 = 70

Constraints

  • a.length == 4
  • 4 ≤ b.length ≤ 105
  • -105 ≤ a[i], b[i] ≤ 105

Visualization

Tap to expand
Maximum Multiplication Score INPUT Array a (size 4) 3 2 -1 1 indices: 0, 1, 2, 3 Array b (size 6) 5 -3 4 2 -1 6 indices: 0, 1, 2, 3, 4, 5 Choose 4 indices from b: i0 < i1 < i2 < i3 Maximize: a[0]*b[i0] + a[1]*b[i1] + a[2]*b[i2] + a[3]*b[i3] ALGORITHM STEPS 1 Define DP State dp[i][j] = max score using first i elements of a, first j elements of b 2 Transition For each b[j]: skip or pair with a[i] dp[i][j] = max(dp[i][j-1], dp[i-1][j-1]+a[i]*b[j]) 3 Optimal Selection i0=0: b[0]=5, a[0]=3 i1=2: b[2]=4, a[1]=2 i2=4: b[4]=-1, a[2]=-1 i3=5: b[5]=6, a[3]=1 4 Calculate Score 15 + 8 + 1 + 6 = 26 O(n) space optimized FINAL RESULT Optimal Indices 5 i0=0 -3 4 i1=2 2 -1 i2=4 6 i3=5 Score Calculation 3 * 5 = 15 2 * 4 = 8 -1*-1 = 1 1 * 6 = 6 Total = 26 OUTPUT 26 OK - Maximum Score Key Insight: This is a classic DP problem where we track the maximum score achievable after pairing the first i elements of array a with i elements from the first j elements of array b. The constraint i0 < i1 < i2 < i3 ensures we process elements in order. Negative values in both arrays require careful consideration: -1 * -1 = +1 adds to our score! TutorialsPoint - Maximum Multiplication Score | Optimal Solution
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
32.8K Views
Medium Frequency
~25 min Avg. Time
840 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