Maximum Number of Operations With the Same Score II - Problem

You're given an array of integers nums, and you need to perform operations to remove pairs of elements. Each operation removes exactly two elements and gives you a score equal to the sum of those elements.

You have three choices for each operation:

  • ๐Ÿ”ธ Remove the first two elements: nums[0] + nums[1]
  • ๐Ÿ”ธ Remove the last two elements: nums[n-2] + nums[n-1]
  • ๐Ÿ”ธ Remove the first and last elements: nums[0] + nums[n-1]

Challenge: Find the maximum number of operations you can perform such that all operations have the same score.

Example: With [3,2,1,4,5], if you choose score 8, you could remove (3,5) โ†’ score 8, then (2,6) โ†’ but 6 isn't available! You need to plan ahead to maximize operations with consistent scoring.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [3,2,1,4,5]
โ€บ Output: 2
๐Ÿ’ก Note: We can choose target score 8. First, remove first and last elements (3,5) โ†’ score 8. Then remove first and last from remaining [2,1,4] โ†’ (2,4) โ†’ score 6. Since 6 โ‰  8, we can't continue. Actually, the optimal is target score 6: remove (2,4) โ†’ score 6, then (3,1) โ†’ score 4. Wait, that doesn't work either. Let's try target 7: remove first two (3,2) โ†’ score 5. That doesn't match target 7. The correct answer is trying target 7 from last two (4,5): remove (4,5) โ†’ score 9, not 7. The actual optimal path gives us 2 operations maximum.
example_2.py โ€” All Same Pairs
$ Input: nums = [3,2,6,1,4]
โ€บ Output: 1
๐Ÿ’ก Note: Possible target scores: 3+2=5, 6+1=7, 4+1=5, 3+4=7. If we choose target 5: we can remove (3,2) for score 5, leaving [6,1,4]. Now we need pairs that sum to 5, but 6+1=7, 6+4=10, 1+4=5. So we can remove (1,4) for score 5, leaving [6]. Since we need at least 2 elements, we stop. That's 2 operations. But let's verify this is actually achievable...
example_3.py โ€” Edge Case
$ Input: nums = [1,1]
โ€บ Output: 1
๐Ÿ’ก Note: With only 2 elements, we can perform exactly 1 operation. The only option is to remove both elements (1,1) with score 2. Result: 1 operation.

Constraints

  • 2 โ‰ค nums.length โ‰ค 2000
  • 1 โ‰ค nums[i] โ‰ค 1000
  • The array will have at least 2 elements for meaningful operations

Visualization

Tap to expand
Maximum Operations StrategyInitial Array: [3, 2, 1, 4, 5]32145Three Possible Target ScoresFirst Two: 3+2=5Try target score 5Last Two: 4+5=9Try target score 9First+Last: 3+5=8Try target score 8๐Ÿง  Key Insight: MemoizationCache results for (left, right, target) statesAvoid recalculating same subproblemsโฑ Time: O(nยฒ) | ๐Ÿ’พ Space: O(nยฒ)๐ŸŽฏ **Key Insight:** Only 3 possible target scores to try, use memoization to cache subproblem results for optimal performance
Understanding the Visualization
1
Choose Target Score
Try each of the 3 possible first moves to set target score
2
Recursive Exploration
For each remaining configuration, try all valid moves matching target
3
Memoize Results
Cache results to avoid recalculating same subproblems
4
Return Maximum
Take the best result across all 3 target scores
Asked in
Google 28 Amazon 22 Meta 18 Microsoft 15
24.6K Views
Medium Frequency
~25 min Avg. Time
892 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