
- 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 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 Articles
- 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
- C++ program to count how many minutes we have to wait to meet at least one swimmer
- C++ program to count number of operations needed to reach n by paying coins
- C++ program to count minimum number of operations needed to make number n to 1
- C++ program to count in how many ways we can paint blocks with two conditions
- C++ Program to count number of operations needed to place elements whose index is smaller than value
- C++ program to find minimum number of operations needed to make all cells at r row c columns black
- C++ program to find minimum how many coins needed to buy binary string
- C++ Program to count operations to make filename valid
- Program to find number of operations needed to decrease n to 0 in C++
- How many ohms are needed for proper earthing?
- Program to count number of operations needed to make string as concatenation of same string twice in Python
- C++ program to calculate how many years needed to get X rupees with 1% interest
- C++ program to count number of cities we can visit from each city with given operations
