Making zero array by decrementing pairs of adjacent | Javascript program to find if it is possible to make all array elements 0 by decrement operations . ; Used for storing the sum of even and odd position element in array . ; If position is odd , store sum value of odd position in odd ; If position is even , store sum value of even position in even ; Driver code
function isPossibleToZero ( a , n ) { let even = 0 , odd = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( i & 1 ) == 0 ) odd += a [ i ] ; else even += a [ i ] ; } return ( odd == even ) ; } let arr = [ 0 , 1 , 1 , 0 ] ; let n = arr . length ; if ( isPossibleToZero ( arr , n ) ) document . write ( " " ) ; else document . write ( " " ) ;