Sum of all elements in an array between zeros | C ++ program for the above approach ; 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
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void sumBetweenZero ( int arr [ ] , int N ) { int i = 0 ; vector < int > A ; int sum = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { i ++ ; break ; } } for ( ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { A . push_back ( sum ) ; sum = 0 ; } else { sum += arr [ i ] ; } } for ( int i = 0 ; i < A . size ( ) ; i ++ ) { cout << A [ i ] << ' ▁ ' ; } if ( A . size ( ) == 0 ) cout << " - 1" ; } int main ( ) { int arr [ ] = { 1 , 0 , 3 , 4 , 0 , 4 , 4 , 0 , 2 , 1 , 4 , 0 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sumBetweenZero ( arr , N ) ; return 0 ; }