- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# 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

- Related Articles
- C++ program to count minimum number of operations needed to make number n to 1
- C++ program to count number of minimum coins needed to get sum k
- C++ program to find minimum number of steps needed to move from start to end
- Program to find number of operations needed to make pairs from first and last side are with same sum in Python
- Program to find number of optimal steps needed to reach destination by baby and giant steps in Python
- Program to count number of operations needed to make string as concatenation of same string twice in Python
- C++ program to count number of stairways and number of steps in each stairways
- Program to find number of coins needed to make the changes in Python
- Program to count minimum deletions needed to make character frequencies unique in Python
- Program to check minimum number of characters needed to make string palindrome in Python
- C++ program to count number of operations needed to reach n by paying coins
- C++ program to count minimum number of binary digit numbers needed to represent n
- Program to find minimum operations needed to make two arrays sum equal in Python
- C++ program to count expected number of operations needed for all node removal
- C++ program to find minimum how many operations needed to make number 0

Advertisements