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
using System ; using System . Collections . Generic ; class GFG { static void sumBetweenZero ( int [ ] arr , int N ) { int i = 0 ; List < int > A = new List < int > ( ) ; 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 . Count ; j ++ ) { Console . Write ( A [ j ] + " ▁ " ) ; } if ( A . Count == 0 ) Console . Write ( " - 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 ) ; } }