We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Maximize Score After Pair Deletions

Number: 3839

Difficulty: Medium

Paid? Yes

Companies: N/A


Problem Description

You are given an array of integers. Until the array has at most two elements, you must repeatedly delete a pair of numbers by choosing one of the following operations:

  • Remove the first two elements.
  • Remove the last two elements.
  • Remove the first and the last element. For each removed pair, add the sum of the two removed elements to your total score. Return the maximum possible score obtainable by performing these operations optimally.

Key Insights

  • Since you only remove elements from either end, the remaining elements are always contiguous in the original array.
  • The total sum of all numbers is fixed; therefore, maximizing the score is equivalent to minimizing the sum of the final remaining elements.
  • If the length of the array is odd, you perform operations until one element remains. Any single element can be the final one, so you want to leave the minimum element.
  • If the length is even, operations stop when two elements remain. The final two elements always form a contiguous pair; thus, you want to leave the contiguous pair with the smallest sum.
  • For arrays with no allowed operations (length ≤ 2), the maximum score is 0.

Space and Time Complexity

Time Complexity: O(n)
Space Complexity: O(1)


Solution

The key observation is that every allowed operation removes items from the ends; hence, after all operations, a contiguous block of the original array remains. Let total = sum(nums). The score accumulated is the total sum minus the sum of the remaining block.

  • When n (the length of nums) is odd, the final remaining block is of length 1 and you are free to choose any element. Therefore, the best option is to leave the minimum element and get a score of total - min(nums).
  • When n is even, the final remaining block is of length 2, which will be a contiguous pair in nums. In this case, you choose the adjacent pair with the minimum sum and the score is total - (minimum pair sum).

This approach reduces the search to a simple linear scan either to find the minimum element (if n is odd) or the minimum sum of any contiguous pair (if n is even). Edge cases where the length is ≤ 2 return 0 because no operation is allowed.


Code Solutions

# Python solution with detailed comments

def maximizeScore(nums):
    # If the array length is less than or equal to 2, no operations can be performed.
    if len(nums) <= 2:
        return 0
    
    total = sum(nums)  # Calculate the total sum of the array.
    n = len(nums)
    
    # If the length of the array is odd:
    if n % 2 == 1:
        # Any single element can be the final one, so choose the minimum element.
        min_remaining = min(nums)
        return total - min_remaining
    else:
        # If the length is even, the final remaining block will be a contiguous pair.
        # Find the contiguous pair with the minimum sum.
        min_pair_sum = float('inf')
        for i in range(n - 1):
            pair_sum = nums[i] + nums[i + 1]
            if pair_sum < min_pair_sum:
                min_pair_sum = pair_sum
        return total - min_pair_sum

# Example test cases
print(maximizeScore([2, 4, 1]))  # Expected output: 6
print(maximizeScore([5, -1, 4, 2]))  # Expected output: 7
← Back to All Questions