Skip to main content
Back to problems
#3496
Medium Algorithms

Maximize score after pair deletions

Array Greedy
52.5% acceptance
Mar 31, 2026
11
3
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn max_score(nums: Vec<i32>) -> i32 {
    let n = nums.len();
    let total: i32 = nums.iter().sum();
    if n <= 2 {
      return 0;
    }
    if n % 2 == 1 {
      // Leave the minimum element
      total - *nums.iter().min().unwrap()
    } else {
      // Leave the adjacent pair with minimum sum
      let min_pair = (0..n - 1).map(|i| nums[i] + nums[i + 1]).min().unwrap();
      total - min_pair
    }
  }
}