Given an array arr[] of N integers, the task is to find the sum of all elements between two zeros in the given array. If possible, then print all the sum, else print “-1”.
Note: There is no continuous zero in the given array.
Examples:
Input: arr[] = { 1, 0, 3, 4, 0, 4, 4, 0, 2, 1, 4, 0, 3 }
Output: 7 8 7
Explanation:
The sum of elements between every zero are:
3 + 4 = 7
4 + 4 = 8
2 + 1 + 4 = 7Input: arr[] = { 1, 3, 4, 6, 0}
Output: -1
Approach:
- Traverse the given array arr[] and find the first index with element 0.
- If any element with value zero occurs, then start storing the sum of elements after it in a vector(say A[]) untill next zero occurs.
- Repeat above steps for every zero occurs.
- Print the elements stored in A[].
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include "bits/stdc++.h" using namespace std; // Function to find the sum between two // zeros in the given array arr[] void sumBetweenZero(int arr[], int N) { int i = 0; // To store the sum of element // between two zeros vector<int> A; // To store the sum int sum = 0; // Find first index of 0 for (i = 0; i < N; i++) { if (arr[i] == 0) { i++; break; } } // Traverse the given array arr[] for (; i < N; i++) { // If 0 occurs then add it to A[] if (arr[i] == 0) { A.push_back(sum); sum = 0; } // Else add element to the sum else { sum += arr[i]; } } // Print all the sum stored in A for (int i = 0; i < A.size(); i++) { cout << A[i] << ' '; } // If there is no such element print -1 if (A.size() == 0) cout << "-1"; } // Driver Code int main() { int arr[] = { 1, 0, 3, 4, 0, 4, 4, 0, 2, 1, 4, 0, 3 }; int N = sizeof(arr) / sizeof(arr[0]); // Function call sumBetweenZero(arr, N); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find the sum between two // zeros in the given array arr[] static void sumBetweenZero(int arr[], int N) { int i = 0; // To store the sum of element // between two zeros Vector<Integer> A = new Vector<Integer>(); // To store the sum int sum = 0; // Find first index of 0 for(i = 0; i < N; i++) { if (arr[i] == 0) { i++; break; } } // Traverse the given array arr[] for(; i < N; i++) { // If 0 occurs then add it to A[] if (arr[i] == 0) { A.add(sum); sum = 0; } // Else add element to the sum else { sum += arr[i]; } } // Print all the sum stored in A for(int j = 0; j < A.size(); j++) { System.out.print(A.get(j) + " "); } // If there is no such element print -1 if (A.size() == 0) System.out.print("-1"); } // Driver Code 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; // Function call sumBetweenZero(arr, N); } } // This code is contributed by gauravrajput1 |
Python3
#Python3 program for the above approach # Function to find the sum between two # zeros in the given array arr[] def sumBetweenZero(arr, N): i = 0 # To store the sum of the element # between two zeros A = [] # To store the sum sum = 0 # Find first index of 0 for i in range(N): if (arr[i] == 0): i += 1 break k = i # Traverse the given array arr[] for i in range(k, N, 1): # If 0 occurs then add it to A[] if (arr[i] == 0): A.append(sum) sum = 0 # Else add element to the sum else: sum += arr[i] # Print all the sum stored in A for i in range(len(A)): print(A[i], end = ' ') # If there is no such element print -1 if (len(A) == 0): print("-1") # Driver Code if __name__ == '__main__': arr = [ 1, 0, 3, 4, 0, 4, 4, 0, 2, 1, 4, 0, 3 ] N = len(arr) # Function call sumBetweenZero(arr, N) # This code is contributed by Bhupendra_Singh |
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to find the sum between two // zeros in the given array []arr static void sumBetweenZero(int []arr, int N) { int i = 0; // To store the sum of element // between two zeros List<int> A = new List<int>(); // To store the sum int sum = 0; // Find first index of 0 for(i = 0; i < N; i++) { if (arr[i] == 0) { i++; break; } } // Traverse the given array []arr for(; i < N; i++) { // If 0 occurs then add it to []A if (arr[i] == 0) { A.Add(sum); sum = 0; } // Else add element to the sum else { sum += arr[i]; } } // Print all the sum stored in A for(int j = 0; j < A.Count; j++) { Console.Write(A[j] + " "); } // If there is no such element print -1 if (A.Count == 0) Console.Write("-1"); } // Driver Code 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; // Function call sumBetweenZero(arr, N); } } // This code is contributed by gauravrajput1 |
7 8 7
Time Complexity: O(N), where N is the length of the array.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Remove leading zeros from an array
- Maximum consecutive one’s (or zeros) in a binary array
- Move all zeros to start and ones to end in an Array of random integers
- Maximum consecutive one’s (or zeros) in a binary circular array
- Count number of trailing zeros in product of array
- Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i]
- Binary Array Range Queries to find the minimum distance between two Zeros
- Check if Array elements can be maximized upto M by adding all elements from another array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Find original array from encrypted array (An array of sums of other elements)
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Average of remaining elements after removing K largest and K smallest elements from array
- Reduce the array by deleting elements which are greater than all elements to its left
- Maximize sum of atmost K elements in array by taking only corner elements | Set 2
- Length of array pair formed where one contains all distinct elements and other all same elements
- Count of Array elements to be divided by 2 to make at least K elements equal
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Merge first two minimum elements of the array until all the elements are greater than K
- Split an array containing N elements into K sets of distinct elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

