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++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find minimum operations
// needed to make the product of any
// two adjacent elements in prefix
// sum array negative
void minOperations(vector<int> a)
{
// Stores the minimum operations
int res = INT_MAX;
int N = a.size();
for (int r = 0; r < 2; r++) {
// Stores the prefix sum
// and number of operations
int sum = 0, ans = 0;
// Traverse the array
for (int i = 0; i < N; i++) {
// Update the value of sum
sum += a[i];
// Check if i+r is odd
if ((i + r) % 2) {
// Check if prefix sum
// is not positive
if (sum <= 0) {
// Update the value of
// ans and sum
ans += -sum + 1;
sum = 1;
}
}
else {
// Check if prefix sum is
// not negative
if (sum >= 0) {
// Update the value of
// ans and sum
ans += sum + 1;
sum = -1;
}
}
}
// Update the value of res
res = min(res, ans);
}
// Print the value of res
cout << res;
}
// Driver Code
int main()
{
vector<int> a{ 1, -3, 1, 0 };
minOperations(a);
return 0;
}
Java
// Java code for the above approach
import java.util.*;
class GFG{
// Function to find minimum operations
// needed to make the product of any
// two adjacent elements in prefix
// sum array negative
static void minOperations(ArrayList<Integer> a)
{
// Stores the minimum operations
int res = Integer.MAX_VALUE;
int N = a.size();
for(int r = 0; r < 2; r++)
{
// Stores the prefix sum
// and number of operations
int sum = 0, ans = 0;
// Traverse the array
for(int i = 0; i < N; i++)
{
// Update the value of sum
sum += a.get(i);
// Check if i+r is odd
if ((i + r) % 2 == 1)
{
// Check if prefix sum
// is not positive
if (sum <= 0)
{
// Update the value of
// ans and sum
ans += -sum + 1;
sum = 1;
}
}
else
{
// Check if prefix sum is
// not negative
if (sum >= 0)
{
// Update the value of
// ans and sum
ans += sum + 1;
sum = -1;
}
}
}
// Update the value of res
res = Math.min(res, ans);
}
// Print the value of res
System.out.print(res);
}
// Driver Code
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);
}
}
// This code is contributed by SURENDRA_GANGWAR
Python3
# python code for the above approach
# // Function to find minimum operations
# // needed to make the product of any
# // two adjacent elements in prefix
# // sum array negative
def minOperations(a):
#Stores the minimum operations
res = 100000000000
N = len(a)
for r in range(0,2):
# Stores the prefix sum
# and number of operations
sum = 0
ans = 0
# Traverse the array
for i in range (0,N):
# Update the value of sum
sum += a[i]
# Check if i+r is odd
if ((i + r) % 2):
# Check if prefix sum
# is not positive
if (sum <= 0):
# Update the value of
# ans and sum
ans += -sum + 1
sum = 1
else:
# Check if prefix sum is
# not negative
if (sum >= 0):
# Update the value of
# ans and sum
ans += sum + 1;
sum = -1;
# Update the value of res
res = min(res, ans)
# // Print the value of res
print(res)
# Driver code
a = [1, -3, 1, 0]
minOperations(a);
# This code is contributed by Stream_Cipher
C#
// C# code for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to find minimum operations
// needed to make the product of any
// two adjacent elements in prefix
// sum array negative
static void minOperations(List<int> a)
{
// Stores the minimum operations
int res = Int32.MaxValue;
int N = a.Count;
for (int r = 0; r < 2; r++)
{
// Stores the prefix sum
// and number of operations
int sum = 0, ans = 0;
// Traverse the array
for (int i = 0; i < N; i++)
{
// Update the value of sum
sum += a[i];
// Check if i+r is odd
if ((i + r) % 2 == 1) {
// Check if prefix sum
// is not positive
if (sum <= 0) {
// Update the value of
// ans and sum
ans += -sum + 1;
sum = 1;
}
}
else {
// Check if prefix sum is
// not negative
if (sum >= 0) {
// Update the value of
// ans and sum
ans += sum + 1;
sum = -1;
}
}
}
// Update the value of res
res = Math.Min(res, ans);
}
// Print the value of res
Console.Write(res);
}
// Driver Code
public static void Main()
{
List<int> a = new List<int>(){ 1, -3, 1, 0 };
minOperations(a);
}
}
// This code is contributed by bgangwar59.
JavaScript
<script>
// JavaScript program for the above approach
// Function to find minimum operations
// needed to make the product of any
// two adjacent elements in prefix
// sum array negative
function minOperations(a)
{
// Stores the minimum operations
let res =Number.MAX_VALUE;
let N = a.length;
for (let r = 0; r < 2; r++) {
// Stores the prefix sum
// and number of operations
let sum = 0, ans = 0;
// Traverse the array
for (let i = 0; i < N; i++) {
// Update the value of sum
sum += a[i];
// Check if i+r is odd
if ((i + r) % 2) {
// Check if prefix sum
// is not positive
if (sum <= 0) {
// Update the value of
// ans and sum
ans += -sum + 1;
sum = 1;
}
}
else {
// Check if prefix sum is
// not negative
if (sum >= 0) {
// Update the value of
// ans and sum
ans += sum + 1;
sum = -1;
}
}
}
// Update the value of res
res = Math.min(res, ans);
}
// Print the value of res
document.write(res);
}
// Driver Code
let a = [1, -3, 1, 0 ];
minOperations(a);
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimum number of operations required to make all the elements positive Given an array A[] of length N. You can apply the below operation: Choose an index i such that (i+1) element exists. After that choose any number X (positive or negative). Then subtract X from the ith element and add it into (i+1)th element.The task is to find the minimum number of operations requir
9 min read
Minimum operations to make sum of neighbouring elements <= X Given an array arr[] of N elements and an integer X, the task is to find the minimum number of operations required to make sum of neighbouring elements less than the given number X. In a single operation, you can choose an element arr[i] and decrease its value by 1.Examples: Input: arr[] = {2, 2, 2}
6 min read
Generate permutation of 1 to N with sum of min of prefix for each element as Y Given two integers N, Y, generate a permutation of length N such that sum of all prefix minimum of that permutation is Y. Example: Input: N = 5, Y = 10Output: 5 2 1 4 3Explanation: Array of prefix minimum for [5, 2, 1, 4, 3] is [5, 2, 1, 1, 1]. Sum of this array of prefix minimum is 10 (= Y). Input:
9 min read
Minimum number of increment operations required to make two Strings equal Given two strings S1 and S2 of the same length containing characters ranging from '0' to '9', the task is to return the minimum number of operations required to make two strings equal by following the below operations: Increment any two characters from the string by 1.If it is not possible to make t
8 min read
Count minimum decrement prefix or suffix or increment all operations to make Array equal to 0 Given an array arr[] of size N. The task is to make all the array elements equal to zero by applying the minimum number of operations. Following operations are allowed: Select an index i and decrease each element by 1 for the prefix up to that index.Select an index i and decrease each element by 1 f
7 min read