Minimize subarray increments / decrements required to reduce all array elements to 0 | Python3 program of the above approach ; Function to count the minimum number of operations required ; Traverse the array ; If array element is negative ; Update minimum negative ; Update maximum position ; Return minOp ; Driver code
import sys NEW_LINE def minOperation ( arr ) : NEW_LINE INDENT minOp = sys . maxsize NEW_LINE minNeg = 0 NEW_LINE maxPos = 0 NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT if ( arr [ i ] < minNeg ) : NEW_LINE INDENT minNeg = arr [ i ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if arr [ i ] > maxPos : NEW_LINE INDENT maxPos = arr [ i ] NEW_LINE DEDENT DEDENT DEDENT return abs ( minNeg ) + maxPos NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 4 , 1 ] NEW_LINE print ( minOperation ( arr ) ) NEW_LINE DEDENT