Minimize subarray increments / decrements required to reduce all array elements to 0 | C ++ Program to implement 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperation ( int arr [ ] , int N ) { int minOp = INT_MIN ; int minNeg = 0 , maxPos = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { if ( arr [ i ] < minNeg ) minNeg = arr [ i ] ; } else { if ( arr [ i ] > maxPos ) maxPos = arr [ i ] ; } } return abs ( minNeg ) + maxPos ; } int main ( ) { int arr [ ] = { 1 , 3 , 4 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minOperation ( arr , N ) ; }