Sum of all elements in an array between zeros
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 = 7
Input: 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 the value zero occurs, then start storing the sum of elements after it in a vector(say A[]) until the next zero occurs.
- Repeat the above steps for every zero that occurs.
- Print the elements stored in A[].
Below is the implementation of the above approach:
C++
#include "bits/stdc++.h"
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;
}
|
Java
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);
}
}
|
Python3
def sumBetweenZero(arr, N):
i = 0
A = []
sum = 0
for i in range (N):
if (arr[i] = = 0 ):
i + = 1
break
k = i
for i in range (k, N, 1 ):
if (arr[i] = = 0 ):
A.append( sum )
sum = 0
else :
sum + = arr[i]
for i in range ( len (A)):
print (A[i], end = ' ' )
if ( len (A) = = 0 ):
print ( "-1" )
if __name__ = = '__main__' :
arr = [ 1 , 0 , 3 , 4 , 0 , 4 , 4 ,
0 , 2 , 1 , 4 , 0 , 3 ]
N = len (arr)
sumBetweenZero(arr, N)
|
C#
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);
}
}
|
Javascript
<script>
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( "-1" );
}
let arr = [1, 0, 3, 4, 0, 4, 4,
0, 2, 1, 4, 0, 3];
let N = arr.length;
sumBetweenZero(arr, N);
</script>
|
Time Complexity: O(N), where N is the length of the array.
Space Complexity: O(N) as ans vector has been created.
Last Updated :
22 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...