Minimum operations to make product of adjacent element pair of prefix sum negative
Last Updated :
02 Jul, 2021
Given an array arr[ ] of size N, consider an array prefix[ ] where prefix[i] is the sum of the first i elements of arr. The task is to find the minimum number of operations required to modify the given array such that the product of any two adjacent elements in the prefix array is negative. In one operation you can increment or decrement the value of any element by 1.
Input: arr[] = {1, -3, 1, 0}
Output: 4
Explanation: The sequence can be transformed into 1, -2, 2, -2 by 4 operations, the sum of the prefixes are 1, -1, 1, -1 which satisfy all the conditions.
Input: arr[] = {-1 4 3 2 -5 4}
Output: 8
Approach: The idea is to try out two independent possibilities that either even length prefix sums are positive and odd length prefix sums are negative or vice versa. Follow the steps below to solve the problem:
- Initialize a variable res = INT_MAX to store the minimum number of operations.
- Traverse over the range [0, 1] using the variable r.
- Initialize variables ans = 0 and sum = 0 to store the count of total operations and the current prefix sum respectively.
- Traverse over the range [0, N-1] using the variable i,
- Add arr[i] to the value of sum.
- If the value of (i+r) is odd and if sum is not positive then add sum+1 to ans and update the value of sum to 1.
- Otherwise if (i+r) is even and if sum is not negative then add sum+1 to ans update the value of sum to -1.
- Update the value of res as min(res, ans).
- After completing the above steps print the value of res.
C++
#include <bits/stdc++.h>
using namespace std;
void minOperations(vector< int > a)
{
int res = INT_MAX;
int N = a.size();
for ( int r = 0; r < 2; r++) {
int sum = 0, ans = 0;
for ( int i = 0; i < N; i++) {
sum += a[i];
if ((i + r) % 2) {
if (sum <= 0) {
ans += -sum + 1;
sum = 1;
}
}
else {
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
}
res = min(res, ans);
}
cout << res;
}
int main()
{
vector< int > a{ 1, -3, 1, 0 };
minOperations(a);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void minOperations(ArrayList<Integer> a)
{
int res = Integer.MAX_VALUE;
int N = a.size();
for ( int r = 0 ; r < 2 ; r++)
{
int sum = 0 , ans = 0 ;
for ( int i = 0 ; i < N; i++)
{
sum += a.get(i);
if ((i + r) % 2 == 1 )
{
if (sum <= 0 )
{
ans += -sum + 1 ;
sum = 1 ;
}
}
else
{
if (sum >= 0 )
{
ans += sum + 1 ;
sum = - 1 ;
}
}
}
res = Math.min(res, ans);
}
System.out.print(res);
}
public static void main(String args[])
{
ArrayList<Integer> a = new ArrayList<Integer>();
a.add( 1 );
a.add(- 3 );
a.add( 1 );
a.add( 0 );
minOperations(a);
}
}
|
Python3
def minOperations(a):
res = 100000000000
N = len (a)
for r in range ( 0 , 2 ):
sum = 0
ans = 0
for i in range ( 0 ,N):
sum + = a[i]
if ((i + r) % 2 ):
if ( sum < = 0 ):
ans + = - sum + 1
sum = 1
else :
if ( sum > = 0 ):
ans + = sum + 1 ;
sum = - 1 ;
res = min (res, ans)
print (res)
a = [ 1 , - 3 , 1 , 0 ]
minOperations(a);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void minOperations(List< int > a)
{
int res = Int32.MaxValue;
int N = a.Count;
for ( int r = 0; r < 2; r++)
{
int sum = 0, ans = 0;
for ( int i = 0; i < N; i++)
{
sum += a[i];
if ((i + r) % 2 == 1) {
if (sum <= 0) {
ans += -sum + 1;
sum = 1;
}
}
else {
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
}
res = Math.Min(res, ans);
}
Console.Write(res);
}
public static void Main()
{
List< int > a = new List< int >(){ 1, -3, 1, 0 };
minOperations(a);
}
}
|
Javascript
<script>
function minOperations(a)
{
let res =Number.MAX_VALUE;
let N = a.length;
for (let r = 0; r < 2; r++) {
let sum = 0, ans = 0;
for (let i = 0; i < N; i++) {
sum += a[i];
if ((i + r) % 2) {
if (sum <= 0) {
ans += -sum + 1;
sum = 1;
}
}
else {
if (sum >= 0) {
ans += sum + 1;
sum = -1;
}
}
}
res = Math.min(res, ans);
}
document.write(res);
}
let a = [1, -3, 1, 0 ];
minOperations(a);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...