Maximum Number of Operations With the Same Score II

Given an array of integers called nums, you can perform any of the following operations while nums contains at least 2 elements:

  • Choose the first two elements of nums and delete them.
  • Choose the last two elements of nums and delete them.
  • Choose the first and the last elements of nums and delete them.

The score of the operation is the sum of the deleted elements.

Your task is to find the maximum number of operations that can be performed, such that all operations have the same score.

Return the maximum number of operations possible that satisfy the condition mentioned above.

Example 1
Inputnums = [3,2,1,2,3,4]
Output3
The operations delete pairs with sums 5: first two, then first and last, then first and last again, for 3 total operations.
Example 2
Inputnums = [3,2,6,1,4]
Output2
Deleting the first two elements and then the last two elements both have score 5, and it can be proven that at most 2 operations are possible.

Constraints

  • 2 <= nums.length <= 2000
  • 1 <= nums[i] <= 1000

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate