Ctrl K

Removals With an Equal Sum

Medium
You are given an integer array values. While the array has at least two elements you may make a move by deleting one of these pairs: - the first two elements; - the last two elements; - the first and the last element. The sum of the deleted pair is the cost of the move. All the moves you make must have the same cost. Return the largest number of moves you can make under this rule.
Example 1
values=[3, 4, 2, 5, 1, 6] → 3

Remove the first two (3 + 4 = 7), then the first two again (2 + 5 = 7), then the remaining 1 and 6 (7 as well). Three moves.

Example 2
values=[5, 1, 8, 2, 4] → 2

Starting with 5 + 4 = 9, the next move takes 1 + 8 = 9 and a single number is left. The other first moves (cost 6) also allow at most two moves.

Example 3
values=[7, 7] → 1

The only possible move removes both numbers.

Constraints
  • 2 ≤ len(values) ≤ 2000
  • 1 ≤ values[i] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSplit Into Dictionary Words MediumCheapest Basket with Bundles MediumGroups With Equal Sums MediumEvery Way to Space the Words HardLongest Uphill Walk Hard
loading Python…
Input
values=[3, 4, 2, 5, 1, 6]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 117 tests on the server, hidden ones included.