Maximize Score After N Operations - Problem
Maximize Score After N Operations
You're playing a strategic number game! Given an array
In each operation
• Choose any two elements
• Calculate your score:
• Remove both elements from the array
Your goal is to strategically choose pairs to maximize the total score across all operations. The catch? Later operations are worth more points (multiplied by the operation number), but you have fewer choices as elements get removed!
Example: With
• Operation 1: Choose (1,5) → Score = 1 × gcd(1,5) = 1 × 1 = 1
• Operation 2: Choose (2,4) → Score = 2 × gcd(2,4) = 2 × 2 = 4
• Operation 3: Choose (3,6) → Score = 3 × gcd(3,6) = 3 × 3 = 9
• Total Score: 1 + 4 + 9 = 14
You're playing a strategic number game! Given an array
nums containing 2*n positive integers, you need to perform exactly n operations to maximize your total score.In each operation
i (starting from 1), you must:• Choose any two elements
x and y from the remaining numbers• Calculate your score:
i × gcd(x, y)• Remove both elements from the array
Your goal is to strategically choose pairs to maximize the total score across all operations. The catch? Later operations are worth more points (multiplied by the operation number), but you have fewer choices as elements get removed!
Example: With
[1, 2, 3, 4, 5, 6] and n=3:• Operation 1: Choose (1,5) → Score = 1 × gcd(1,5) = 1 × 1 = 1
• Operation 2: Choose (2,4) → Score = 2 × gcd(2,4) = 2 × 2 = 4
• Operation 3: Choose (3,6) → Score = 3 × gcd(3,6) = 3 × 3 = 9
• Total Score: 1 + 4 + 9 = 14
Input & Output
example_1.py — Python
$
Input:
[1, 2, 3, 4, 5, 6]
›
Output:
14
💡 Note:
One optimal strategy: Operation 1: choose (1,5) → score = 1×gcd(1,5) = 1×1 = 1. Operation 2: choose (2,4) → score = 2×gcd(2,4) = 2×2 = 4. Operation 3: choose (3,6) → score = 3×gcd(3,6) = 3×3 = 9. Total = 1+4+9 = 14
example_2.py — Python
$
Input:
[3, 4, 6, 8]
›
Output:
11
💡 Note:
Operation 1: choose (3,6) → score = 1×gcd(3,6) = 1×3 = 3. Operation 2: choose (4,8) → score = 2×gcd(4,8) = 2×4 = 8. Total = 3+8 = 11
example_3.py — Python
$
Input:
[1, 1]
›
Output:
1
💡 Note:
Only one operation possible: choose (1,1) → score = 1×gcd(1,1) = 1×1 = 1
Visualization
Tap to expand
Understanding the Visualization
1
Setup
You have 2n cards with numbers, need to make n pairs
2
Strategy
Later rounds have higher multipliers, so save high-GCD pairs for later
3
Execution
Use DP with bitmasks to try all strategies efficiently
4
Optimization
Memoize results to avoid recalculating the same game states
Key Takeaway
🎯 Key Insight: Use dynamic programming with bitmasks to efficiently explore all pairing strategies while memoizing results to avoid redundant calculations.
Time & Space Complexity
Time Complexity
O(4^n)
There are 2^(2n) possible states, and for each state we try O(n^2) pairs
✓ Linear Growth
Space Complexity
O(2^(2n))
We need to store the result for each possible bitmask state
⚡ Linearithmic Space
Constraints
- 1 ≤ n ≤ 7 (so 2 ≤ nums.length ≤ 14)
- 1 ≤ nums[i] ≤ 106
- nums.length == 2 * n (always even number of elements)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code