Sum of all elements in an array between zeros | Function to find the sum between two zeros in the given array arr [ ] ; To store the sum of element between two zeros ; To store the sum ; Find first index of 0 ; Traverse the given array arr [ ] ; If 0 occurs then add it to A [ ] ; Else add element to the sum ; Print all the sum stored in A ; If there is no such element print - 1 ; Driver Code ; Function call
function sumBetweenZero ( arr , N ) { let i = 0 ; let A = new Array ( ) ; let sum = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { i ++ ; break ; } } for ( ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { A . push ( sum ) ; sum = 0 ; } else { sum += arr [ i ] ; } } for ( let i = 0 ; i < A . length ; i ++ ) { document . write ( A [ i ] + ' ' ) ; } if ( A . length == 0 ) document . write ( " " ) ; } let arr = [ 1 , 0 , 3 , 4 , 0 , 4 , 4 , 0 , 2 , 1 , 4 , 0 , 3 ] ; let N = arr . length ; sumBetweenZero ( arr , N ) ;