Minimum increment/decrement operations required on Array to satisfy given conditions

Given an array arr[] of size N, the task is to find the minimum number of increment or decrement operations required at any index i such that for each i (1 ≤ i < N) if the sum of elements at index from 1 to i is positive then the sum of elements from 1 to i + 1 must be negative or vice versa.
Note: Consider array as 1-based indexing.

Examples:

Input: arr[] = {3, -4, 5, 0, 1}
Output: 6
Explanation: 
Convert the array as {3, -4, 5, -5, 2}. Here, the sum of elements till i is represented as si.
For i = 1, s1 = 3 and s2 = 3 + (-4) = -1. s1 is positive and s2 is negative.
For i=2, s2 = -1 and s3 = 3 + (-4) + 5 = 4. s2 is negative and s3 is positive.
For i = 3, s3 = 4 and s4 = 3 + (-4) + 5 + (-5) = -1. s3 is positive and s4 is negative.
For i = 4, s4 = -1 and s5 = 3 + (-4) + 5 +(-5) + 2 = 1. s4 is negative and s5 is positive.

Input: arr[] = {1, -2, 2, -3}
Output: 0
Explanation: 
Given array already satisfies the condition. Therefore, no need to perform any operation. 

Approach: The array will satisfy the conditions if for each i from 1 to N – 1:



  • If i is odd, then the sum of elements from 1 to i is positive.
  • If i is even, then the sum of elements from 1 to i is negative and vice versa.

Try both the above possibilities and choose the one which gives the minimum number of operations. Below are the steps:

  1. Initialize a variable num_of_ops = 0 which marks the number of operations done so far.
  2. For any index i, if i is even and the sum of elements from 1 to i is negative, then add (1+|sum|) in the arr[i] to make it positive. Now the sum of elements from 1 to i will be 1. Also add (1+|sum|) in the num_of_ops i.e., to count the number of operations.
  3. If i is odd and the sum of elements from 1 to i is positive,  then subtract (1+|sum|) from a[i] to make it negative. Now the sum of elements from 1 to i  will be -1. Also add (1+|sum|) in the num_of_ops. i.e., to count the number of operations.
  4. Similarly, find the number of operations taking for even i, the sum of elements till i is negative and for odd i sum of elements till i is positive.
  5. Choose the minimum number of operations from the above two procedures.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <iostream>
using namespace std;
  
// Function to find minimum number
// of operations to get desired array
int minOperations(int a[], int N)
{
    int num_of_ops1, num_of_ops2, sum;
    num_of_ops1 = num_of_ops2 = sum = 0;
  
    // For even 'i', sum of
    // elements till 'i' is negative
  
    // For odd 'i', sum of
    // elements till 'i' is positive
    for (int i = 0; i < N; i++) {
        sum += a[i];
  
        // If i is even and sum is positive,
        // make it negative by subtracting
        // 1 + |s| from a[i]
        if (i % 2 == 0 && sum >= 0) {
            num_of_ops1 += (1 + abs(sum));
            sum = -1;
        }
  
        // If i is odd and sum is negative,
        // make it positive by
        // adding 1 + |s| into a[i]
        else if (i % 2 == 1 && sum <= 0) {
            num_of_ops1 += (1 + abs(sum));
            sum = 1;
        }
    }
  
    sum = 0;
  
    // For even 'i', the sum of
    // elements till 'i' is positive
  
    // For odd 'i', sum of
    // elements till 'i' is negative
    for (int i = 0; i < N; i++) {
        sum += a[i];
  
        // Check if 'i' is odd and sum is
        // positive, make it negative by
        // subtracting  1 + |s| from a[i]
        if (i % 2 == 1 && sum >= 0) {
            num_of_ops2 += (1 + abs(sum));
            sum = -1;
        }
  
        // Check if 'i' is even and sum
        // is negative, make it positive
        // by adding 1 + |s| into a[i]
        else if (i % 2 == 0 && sum <= 0) {
            num_of_ops2 += (1 + abs(sum));
            sum = 1;
        }
    }
  
    // Return the minimum of the two
    return min(num_of_ops1, num_of_ops2);
}
  
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 3, -4, 5, 0, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    cout << minOperations(arr, N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.*;
class GFG{
  
// Function to find minimum number
// of operations to get desired array
static int minOperations(int a[], int N)
{
    int num_of_ops1, num_of_ops2, sum;
    num_of_ops1 = num_of_ops2 = sum = 0;
  
    // For even 'i', sum of
    // elements till 'i' is negative
  
    // For odd 'i', sum of
    // elements till 'i' is positive
    for (int i = 0; i < N; i++) 
    {
        sum += a[i];
  
        // If i is even and sum is positive,
        // make it negative by subtracting
        // 1 + |s| from a[i]
        if (i % 2 == 0 && sum >= 0
        {
            num_of_ops1 += (1 + Math.abs(sum));
            sum = -1;
        }
  
        // If i is odd and sum is negative,
        // make it positive by
        // adding 1 + |s| into a[i]
        else if (i % 2 == 1 && sum <= 0
        {
            num_of_ops1 += (1 + Math.abs(sum));
            sum = 1;
        }
    }
  
    sum = 0;
  
    // For even 'i', the sum of
    // elements till 'i' is positive
  
    // For odd 'i', sum of
    // elements till 'i' is negative
    for (int i = 0; i < N; i++) 
    {
        sum += a[i];
  
        // Check if 'i' is odd and sum is
        // positive, make it negative by
        // subtracting  1 + |s| from a[i]
        if (i % 2 == 1 && sum >= 0
        {
            num_of_ops2 += (1 + Math.abs(sum));
            sum = -1;
        }
  
        // Check if 'i' is even and sum
        // is negative, make it positive
        // by adding 1 + |s| into a[i]
        else if (i % 2 == 0 && sum <= 0)
        {
            num_of_ops2 += (1 + Math.abs(sum));
            sum = 1;
        }
    }
  
    // Return the minimum of the two
    return Math.min(num_of_ops1, num_of_ops2);
}
  
// Driver Code
public static void main(String[] args)
{
    // Given array arr[]
    int arr[] = { 3, -4, 5, 0, 1 };
    int N = arr.length;
  
    // Function Call
    System.out.print(minOperations(arr, N));
}
}
  
// This code is contributed by Amit Katiyar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
  
# Function to find minimum number
# of operations to get desired array
def minOperations(a, N):
  
    num_of_ops1 = num_of_ops2 = sum = 0;
  
    # For even 'i', sum of
    # elements till 'i' is negative
  
    # For odd 'i', sum of
    # elements till 'i' is positive
    for i in range(N):
        sum += a[i]
  
        # If i is even and sum is positive,
        # make it negative by subtracting
        # 1 + |s| from a[i]
        if (i % 2 == 0 and sum >= 0):
            num_of_ops1 += (1 + abs(sum))
            sum = -1
  
        # If i is odd and sum is negative,
        # make it positive by
        # adding 1 + |s| into a[i]
        elif (i % 2 == 1 and sum <= 0):
            num_of_ops1 += (1 + abs(sum))
            sum = 1
  
    sum = 0
  
    # For even 'i', the sum of
    # elements till 'i' is positive
  
    # For odd 'i', sum of
    # elements till 'i' is negative
    for i in range (N):
        sum += a[i]
  
        # Check if 'i' is odd and sum is
        # positive, make it negative by
        # subtracting 1 + |s| from a[i]
        if (i % 2 == 1 and sum >= 0):
            num_of_ops2 += (1 + abs(sum))
            sum = -1
  
        # Check if 'i' is even and sum
        # is negative, make it positive
        # by adding 1 + |s| into a[i]
        elif (i % 2 == 0 and sum <= 0):
            num_of_ops2 += (1 + abs(sum))
            sum = 1
  
    # Return the minimum of the two
    return min(num_of_ops1, num_of_ops2)
  
# Driver Code
if __name__ == "__main__":
      
    # Given array arr[]
    arr = [ 3, -4, 5, 0, 1 ]
    N = len(arr)
  
    # Function call
    print(minOperations(arr, N))
  
# This code is contributed by chitranayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
  
class GFG{
  
// Function to find minimum number
// of operations to get desired array
static int minOperations(int []a, int N)
{
    int num_of_ops1, num_of_ops2, sum;
    num_of_ops1 = num_of_ops2 = sum = 0;
  
    // For even 'i', sum of
    // elements till 'i' is negative
  
    // For odd 'i', sum of
    // elements till 'i' is positive
    for(int i = 0; i < N; i++) 
    {
        sum += a[i];
  
        // If i is even and sum is positive,
        // make it negative by subtracting
        // 1 + |s| from a[i]
        if (i % 2 == 0 && sum >= 0) 
        {
            num_of_ops1 += (1 + Math.Abs(sum));
            sum = -1;
        }
  
        // If i is odd and sum is negative,
        // make it positive by
        // adding 1 + |s| into a[i]
        else if (i % 2 == 1 && sum <= 0) 
        {
            num_of_ops1 += (1 + Math.Abs(sum));
            sum = 1;
        }
    }
  
    sum = 0;
  
    // For even 'i', the sum of
    // elements till 'i' is positive
  
    // For odd 'i', sum of
    // elements till 'i' is negative
    for(int i = 0; i < N; i++) 
    {
        sum += a[i];
  
        // Check if 'i' is odd and sum is
        // positive, make it negative by
        // subtracting 1 + |s| from a[i]
        if (i % 2 == 1 && sum >= 0) 
        {
            num_of_ops2 += (1 + Math.Abs(sum));
            sum = -1;
        }
  
        // Check if 'i' is even and sum
        // is negative, make it positive
        // by adding 1 + |s| into a[i]
        else if (i % 2 == 0 && sum <= 0)
        {
            num_of_ops2 += (1 + Math.Abs(sum));
            sum = 1;
        }
    }
  
    // Return the minimum of the two
    return Math.Min(num_of_ops1, num_of_ops2);
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Given array []arr
    int []arr = { 3, -4, 5, 0, 1 };
    int N = arr.Length;
  
    // Function call
    Console.Write(minOperations(arr, N));
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Output: 

6

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.