Official algorithmic problem description and constraints.
An array A
A of size N(N≥2)
N(N≥2), containing non-negative integers, is called good if all of the following conditions hold:
Less formally, we need all non-zero Ai
Ai
equal to the sum of it's adjacent elements.
You are given an array A
A of size N
N, consisting of non-negative integers.
In one operation, you can add 1
1 or subtract 1
1 from some element in A
A. It is not allowed to make the elements in A
A negative at any point.
Find the minimum operations to make the array A
A good. It can be proven that it is always possible.
For each test case, output on a new line the minimum number of operations to make A
A good.
Input
Output
6 2 0 5 2 3 4 3 0 0 0 3 2 2 1 5 6 7 2 1 2 9 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 5 1 0 1 4 3000000000
Test Case 1 : We can reduce A2
A2
by 5
5 to get [0,0]
[0,0] which is good. We could also have increased A1
A1
by 5
5 to get [5,5]
[5,5] which is also good.
Test Case 2 : We can reduce A2
A2
by 1
1 to get [3,3]
[3,3] which is good.
Detailed video explanations, mathematical intuition, and clean C++ implementation code.