Minimize subarray increments / decrements required to reduce all array elements to 0 | Java 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 positive ; Return minOp ; Driver Code
import java . util . * ; import java . lang . * ; class GFG { static int minOperation ( int [ ] arr ) { int minOp = Integer . MIN_VALUE ; int minNeg = 0 , maxPos = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] < 0 ) { if ( arr [ i ] < minNeg ) minNeg = arr [ i ] ; } else { if ( arr [ i ] > maxPos ) maxPos = arr [ i ] ; } } return Math . abs ( minNeg ) + maxPos ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 4 , 1 } ; System . out . println ( minOperation ( arr ) ) ; } }