# C++ program to count number of steps needed to make sum and the product different from zero

Suppose we have an array A with n elements. In one operation, we can add 1 with any one element preset in A. If either the sum or the product of all elements in the array is equal to zero, We can do this operation one more time. We have to count the minimum number of steps needed to make both the sum and the product of all elements in the array different from zero?

So, if the input is like A = [-1, 0, 0, 1], then the output will be 2, because both product and sum are 0. If we add 1 to the second and the third element, the array will be [−1,1,1,1], the sum will be equal to 2 and the product will be equal to −1.

## Steps

To solve this, we will follow these steps −

sum := 0
cnt := 0
n := size of A
for initialize i := 0, when i < n, update (increase i by 1), do:
x := A[i]
sum := sum + x
cnt := cnt + (if x is same as 0, then 1, otherwise 0)
return (if sum + cnt is same as 0, then cnt + 1, otherwise cnt)

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;

int solve(vector<int> A) {
int sum = 0, cnt = 0;
int n = A.size();
for (int i = 0; i < n; i++) {
int x = A[i];
sum += x;
cnt += x == 0 ? 1 : 0;
}
return sum + cnt == 0 ? cnt + 1 : cnt;
}
int main() {
vector<int> A = { -1, 0, 0, 1 };
cout << solve(A) << endl;
}

## Input

{ -1, 0, 0, 1 }

## Output

2