Minimize increments or decrements required to make sum and product of array elements non | Function to find the sum of array ; Return the sum ; Function that counts the minimum operations required to make the sum and product of array non - zero ; Stores count of zero elements ; Iterate over the array to count zero elements ; Sum of elements of the array ; Prvar the result ; Given array arr ; Size of array ; Function Call
function array_sum ( arr , n ) { var sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } function countOperations ( arr , N ) { var count_zeros = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) count_zeros ++ ; } var sum = array_sum ( arr , N ) ; if ( count_zeros != 0 ) return count_zeros ; if ( sum == 0 ) return 1 ; return 0 ; } var arr = [ - 1 , - 1 , 0 , 0 ] ; var N = arr . length ; document . write ( countOperations ( arr , N ) ) ;