Make all elements zero by decreasing any two elements by one at a time | Function that returns true if all the array elements can be made 0 with the given operation ; Find the maximum element and the sum ; Check the required condition ; Driver code
function checkZeroArray ( arr , n ) { let sum = 0 , maximum = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { sum = sum + arr [ i ] ; maximum = Math . max ( maximum , arr [ i ] ) ; } if ( sum % 2 == 0 && maximum <= sum / 2 ) return true ; return false ; } let arr = [ 1 , 2 , 1 , 2 , 2 ] ; let n = arr . length ; if ( checkZeroArray ( arr , n ) == true ) document . write ( " " ) ; else document . write ( " " ) ;