values. In one operation you may take two adjacent elements and replace them with a single element equal to their sum. For example, [3, 1, 2, 5] can become [3, 3, 5].
Return the smallest number of operations after which the array is a palindrome, meaning it reads the same from left to right and from right to left.values=[3, 1, 2, 5, 3] → 2
Merge 1 and 2 to get [3, 3, 5, 3], then merge the second 3 with 5 to get [3, 8, 3]. That is a palindrome after two operations.
values=[6, 2, 6] → 0
The array is already a palindrome, so no operations are needed.
values=[1, 4, 2] → 2
The ends can never be equalised without merging everything into a single number: two operations.
values=[3, 1, 2, 5, 3]
2
Run checks the first 3 tests in your browser. Submit checks all 106 tests on the server, hidden ones included.