- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 Questions & Answers
- C++ program to count minimum number of operations needed to make number n to 1
- C++ program to find minimum number of steps needed to move from start to end
- C++ program to count number of minimum coins needed to get sum k
- Program to find number of optimal steps needed to reach destination by baby and giant steps in Python
- Program to find number of operations needed to make pairs from first and last side are with same sum 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 number of operations needed to make string as concatenation of same string twice 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
- Counting steps to make number palindrome in JavaScript
- How to find the minimum number of steps needed by knight to reach the destination using C#?
- 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

Advertisements