- 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 at least how many operations needed for given conditions

Suppose we have an array A with N elements. In each operation, we pick an element and increase or decrease it by 1. We have to find at least how many operations are needed to satisfy following conditions −

For every i in range 1 to n, the sum of the terms from 1st through ith term is not 0

For every i in range 1 to n - 1, the sign of the terms from the 1st through ith term is different from sign of the sum of the terms from the 1st through (i+1)th term.

So, if the input is like A = [1, -3, 1, 0], then the output will be 4, because we can transform the sequence like 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1.

## Steps

To solve this, we will follow these steps −

n := size of A ret := 0 sum := 0 for each ai in A, do nsum := sum + ai if s > 0, then: if nsum <= 0, then: ret := ret + |nsum| ai := ai + |nsum| Otherwise if nsum >= 0, then: ret := ret + nsum + 1 ai := ai - (nsum + 1) sum := sum + ai s := s * -1 return ret

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; int util(vector<int> A, int s){ int n = A.size(); int ret = 0; int sum = 0; for (int ai : A){ int nsum = sum + ai; if (s > 0){ if (nsum <= 0){ ret += abs(nsum) + 1; ai = ai + abs(nsum) + 1; } } else{ if (nsum >= 0){ ret += nsum + 1; ai = ai - (nsum + 1); } } sum += ai; s *= -1; } return ret; } int solve(vector<int> A){ int res = min(util(A, 1), util(A, -1)); return res; } int main(){ vector<int> A = { 1, -3, 1, 0 }; cout << solve(A) << endl; }

## Input

{ 1, -3, 1, 0 }

## Output

4

- Related Questions & Answers
- C++ program to find minimum how many operations needed to make number 0
- C++ program to count expected number of operations needed for all node removal
- C++ program to count how many minutes we have to wait to meet at least one swimmer
- How many ohms are needed for proper earthing?
- C++ program to count number of operations needed to reach n by paying coins
- C++ program to count in how many ways we can paint blocks with two conditions
- C++ program to count minimum number of operations needed to make number n to 1
- Program to find how many swaps needed to sort an array in Python
- C++ program to find minimum how many coins needed to buy binary string
- C++ Program to count number of operations needed to place elements whose index is smaller than value
- Program to count number of operations needed to make string as concatenation of same string twice in Python
- C++ program to find minimum number of operations needed to make all cells at r row c columns black
- Python Program to Extract Strings with at least given number of characters from other list
- C++ program to calculate how many years needed to get X rupees with 1% interest
- Program to find length of longest substring with character count of at least k in Python