Maximize Score After Pair Deletions - Problem

You are given an array of integers nums. You must repeatedly perform one of the following operations while the array has more than two elements:

  • Remove the first two elements.
  • Remove the last two elements.
  • Remove the first and last element.

For each operation, add the sum of the removed elements to your total score.

Return the maximum possible score you can achieve.

Input & Output

Example 1 — Basic Case
$ Input: nums = [6,2,3,4]
Output: 15
💡 Note: Multiple ways to achieve score 15: Remove [6,2] (score 8), then [3,4] (score 7). Total: 8+7=15. Or remove [3,4] first (score 7), then [6,2] (score 8). Both give maximum score 15.
Example 2 — Small Array
$ Input: nums = [2,7,9,4,5]
Output: 27
💡 Note: Optimal sequence: Remove [2,5] (first+last, score 7), then [7,4] (first+last, score 11), leaving [9] (score 9). Total: 7+11+9=27.
Example 3 — Minimum Size
$ Input: nums = [1,2]
Output: 3
💡 Note: Array has exactly 2 elements, so we sum them all: 1+2=3. No operations are performed since we need more than 2 elements to operate.

Constraints

  • 2 ≤ nums.length ≤ 103
  • -106 ≤ nums[i] ≤ 106

Visualization

Tap to expand
Maximize Score After Pair Deletions INPUT Array: nums 6 i=0 2 i=1 3 i=2 4 i=3 3 Operations: 1. Remove first two 2. Remove last two 3. Remove first + last nums = [6, 2, 3, 4] Length: 4 elements ALGORITHM STEPS 1 Sum all elements Total = 6+2+3+4 = 15 2 Observe pattern Every element removed once 3 Key realization All pairs sum = total sum 4 Return sum Answer = sum(nums) Example Trace: Op1: Remove [6,4] +10 Array: [2,3] Op2: Remove [2,3] +5 Total Score: 15 FINAL RESULT Maximum Score Achieved 15 Verification: sum([6,2,3,4]) = 15 Any valid sequence: - [6,4] then [2,3] - [6,2] then [3,4] - [3,4] then [6,2] All give score = 15 OK Key Insight: The maximum score equals the sum of ALL elements in the array. Regardless of which operations you choose, every element gets removed exactly once and added to the score. TutorialsPoint - Maximize Score After Pair Deletions | Optimal Solution
Asked in
Google 15 Facebook 12 Amazon 8
18.5K Views
Medium Frequency
~25 min Avg. Time
750 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