Given an array arr[] of measurement N. The duty is to make all of the array components equal to zero by making use of the minimal variety of operations.
Following operations are allowed:
 Choose an index i and reduce every ingredient by 1 for the prefix as much as that index.
 Choose an index i and reduce every ingredient by 1 for the suffix ranging from that index.
 Enhance all the weather of the array by 1.
Examples:
Enter: arr[] = {2, 4, 6, 3, 7}
Output: Minimal operations: 12
Clarification: Choose Index 0, and reduce all the weather from
index 1 to 4 by 2 in 2 operations, new arr[] can be {2, 2, 4, 1, 5}.
Choose Index 1, and reduce all the weather from index 2 to 4
by 2 in 2 operations, new arr[] can be {2, 2, 2, 1, 3}
Choose Index 3, and reduce all the weather from index 0 to 2
by 3 in 3 operations, new arr[] can be {1, 1, 1, 1, 3}
Choose Index 3, and reduce ingredient of index 4 by 4 in 4 operations,
New arr[] can be {1, 1, 1, 1, 1}
Enhance all the weather by 1 in 1 operation,
Last arr[] can be {0, 0, 0, 0, 0}
Whole, operations can be 2 + 2 + 3 + 4 + 1 = 12Enter: arr[] = {1, 3, 5, 7, 5, 2, 8}
Output: Minimal operations: 21
Method:
The next downside could be solved utilizing Grasping Method.
Initially make all components equal to the primary ingredient after which decrement them to have worth as 0.
To do that following steps could be taken:
 For making all the weather equal to the primary ingredient, we’ll use the distinction between the consecutive components. ( arr[i+1] – arr[i] ).
 If the distinction (diff = arr[i+1] – arr[i]) is constructive, lower the weather of suffix ranging from index (i+1).
 Variety of operations required = abs( diff )
 If the distinction (diff = arr[i+1] – arr[i]) is detrimental, lower the weather of prefix as much as index i.
 Variety of operations required = abs( diff ) . Reducing the prefix will even lower the primary ingredient of the array by abs( diff).
 Lastly, the full variety of required operations will likely be operations calculated by means of absolutely the distinction between the consecutive components of the array plus absolutely the worth of the primary ingredient of the array.
Beneath is the implementation of the above strategy:
C++14

Minimal operations: 12
Time Complexity: O(N)
Auxiliary House: O(1)