Check if all array elements can be reduced to 0 by repeatedly reducing pairs of consecutive elements by their minimum | Function to check if it is possible to convert the array ; Traverse the array range [ 1 , N - 1 ] ; If arr [ i ] < arr [ i - 1 ] ; Otherwise ; Decrement arr [ i ] by arr [ i - 1 ] ; If arr [ n - 1 ] is not equal to zero ; Otherwise ; Driver Code ; Function Call
function checkPossible ( arr , n ) { for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { document . write ( " " ) ; return ; } else { arr [ i ] -= arr [ i - 1 ] ; arr [ i - 1 ] = 0 ; } } if ( arr [ n - 1 ] == 0 ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } var arr = [ 2 , 3 , 3 , 4 , 2 ] ; var N = arr . length ; checkPossible ( arr , N ) ;