Sum of all elements in an array between zeros | Java 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
import java . util . * ; class GFG { static void sumBetweenZero ( int arr [ ] , int N ) { int i = 0 ; Vector < Integer > A = new Vector < Integer > ( ) ; int sum = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { i ++ ; break ; } } for ( ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { A . add ( sum ) ; sum = 0 ; } else { sum += arr [ i ] ; } } for ( int j = 0 ; j < A . size ( ) ; j ++ ) { System . out . print ( A . get ( j ) + " ▁ " ) ; } if ( A . size ( ) == 0 ) System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 3 , 4 , 0 , 4 , 4 , 0 , 2 , 1 , 4 , 0 , 3 } ; int N = arr . length ; sumBetweenZero ( arr , N ) ; } }