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:
- Initialize a variable num_of_ops = 0 which marks the number of operations done so far.
- 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.
- 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.
- 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.
- Choose the minimum number of operations from the above two procedures.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <iostream>using namespace std;// Function to find minimum number// of operations to get desired arrayint 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 Codeint 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;} |
Java
// Java program for the above approachimport java.util.*;class GFG{// Function to find minimum number// of operations to get desired arraystatic 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 Codepublic 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 |
Python3
# Python3 program for the above approach# Function to find minimum number# of operations to get desired arraydef 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 Codeif __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 |
C#
// C# program for the above approachusing System;class GFG{// Function to find minimum number// of operations to get desired arraystatic 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 Codepublic 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 |
Javascript
<script>// Javascript program for the above approach// Function to find minimum number// of operations to get desired arrayfunction minOperations(a, N){ var 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(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(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// Given array arrvar arr = [ 3, -4, 5, 0, 1 ];var N = arr.length;// Function Calldocument.write(minOperations(arr, N));// This code is contributed by aashish1995</script> |
6
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


