Magicsheet logo

Maximum Number of Operations With the Same Score II

Medium
37.5%
Updated 8/1/2025

Maximum Number of Operations With the Same Score II

What is this problem about?

The Maximum Number of Operations With the Same Score II coding problem is a more complex version of "I". Given an array of integers nums, you can perform an operation by selecting two elements, nums[i] and nums[j], removing them, and recording their sum. You want to find the maximum number of operations you can perform such that all recorded sums are equal. Unlike "I", the elements don't have to be adjacent, and the target sum is not fixed by the first two elements but can be any achievable sum.

Why is this asked in interviews?

Apple and Microsoft ask this problem to test dynamic programming with memoization, especially in scenarios where the "target" value is not immediately obvious. The key insight is that since all sums must be equal, we can try three possible initial sums (first two, first and last, last two) and then use DP to check the maximum operations for each, finding the overall maximum.

Algorithmic pattern used

Dynamic Programming with Memoization: Since all operations must result in the same score, there are only three possible values for this common score: nums[0] + nums[1], nums[0] + nums[n-1], or nums[n-2] + nums[n-1]. We can define a recursive function solve(left, right, target) which returns the maximum operations possible on the subarray nums[left...right] given a fixed target sum.

Base cases:

  • If left >= right, return 0 (no elements or one element, no more operations).

Recursive step:

  • res = 0
  • Try removing nums[left] and nums[left+1]: if nums[left] + nums[left+1] == target, then res = max(res, 1 + solve(left+2, right, target)).
  • Try removing nums[left] and nums[right]: if nums[left] + nums[right] == target, then res = max(res, 1 + solve(left+1, right-1, target)).
  • Try removing nums[right-1] and nums[right]: if nums[right-1] + nums[right] == target, then res = max(res, 1 + solve(left, right-2, target)).

Memoize the results of solve(left, right, target) to avoid recomputing. The target can be passed explicitly or inferred. Since there are only three possible targets, we can run the DP three times.

The state for DP would be dp[left][right]. The final answer would be max(solve(0, n-1, nums[0]+nums[1]), solve(0, n-1, nums[0]+nums[n-1]), solve(0, n-1, nums[n-2]+nums[n-1])).

Example explanation

nums = [3, 2, 1, 2, 3, 4] n = 6.

Possible target sums:

  1. nums[0] + nums[1] = 3 + 2 = 5
  2. nums[0] + nums[5] = 3 + 4 = 7
  3. nums[4] + nums[5] = 3 + 4 = 7

Let's try target = 5:

  • solve(0, 5, 5):
    • Take (3,2) -> 1 + solve(2,5,5)
      • solve(2,5,5) on [1,2,3,4]
        • Take (1,2)? Sum=3 != 5.
        • Take (1,4)? Sum=5. -> 1 + solve(3,4,5)
          • solve(3,4,5) on [2,3]
            • Take (2,3)? Sum=5. -> 1 + solve(5,4,5) -> 1+0 = 1. So for [2,3] gives 1 op. So for [1,2,3,4] it gives 1+1=2 ops.
        • Take (3,4)? Sum=7 != 5. So, for target 5, we can get 1 + 2 = 3 operations. (e.g., (3,2), (1,4), (2,3)).

Let's try target = 7:

  • solve(0, 5, 7):
    • Take (3,4) (nums[0], nums[5]) -> 1 + solve(1,4,7)
      • solve(1,4,7) on [2,1,2,3]
        • Take (2,3) (nums[1], nums[4]) -> 1 + solve(2,3,7)
          • solve(2,3,7) on [1,2]
            • Take (1,2)? Sum=3 != 7. So, for [1,2] it gives 0 ops. So, for [2,1,2,3] it gives 1+0=1 ops. So, for target 7, we can get 1+1=2 operations.

Max is 3.

Common mistakes candidates make

  • Not identifying the limited set of possible target sums: This is the crucial optimization. Without it, the target would be another DP state, making it too complex.
  • Incorrect DP state transitions: Ensure left and right indices are updated correctly after each choice (e.g., left+2, right for removing left, left+1).
  • Missing memoization: A recursive solution without memoization will TLE due to repeated computations.

Interview preparation tip

For the Array Memoization Dynamic Programming interview pattern, always look for problems where the "optimal substructure" (overlapping subproblems) and "optimal choice" (greedy choice reduces problem to subproblem) are present. When the target value is implicit, consider what fixed values it must be.

Similar Questions