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++
#include <iostream>
using namespace std;
int minOperations( int a[], int N)
{
int num_of_ops1, num_of_ops2, sum;
num_of_ops1 = num_of_ops2 = sum = 0;
for ( int i = 0; i < N; i++) {
sum += a[i];
if (i % 2 == 0 && sum >= 0) {
num_of_ops1 += (1 + abs (sum));
sum = -1;
}
else if (i % 2 == 1 && sum <= 0) {
num_of_ops1 += (1 + abs (sum));
sum = 1;
}
}
sum = 0;
for ( int i = 0; i < N; i++) {
sum += a[i];
if (i % 2 == 1 && sum >= 0) {
num_of_ops2 += (1 + abs (sum));
sum = -1;
}
else if (i % 2 == 0 && sum <= 0) {
num_of_ops2 += (1 + abs (sum));
sum = 1;
}
}
return min(num_of_ops1, num_of_ops2);
}
int main()
{
int arr[] = { 3, -4, 5, 0, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << minOperations(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minOperations( int a[], int N)
{
int num_of_ops1, num_of_ops2, sum;
num_of_ops1 = num_of_ops2 = sum = 0 ;
for ( int i = 0 ; i < N; i++)
{
sum += a[i];
if (i % 2 == 0 && sum >= 0 )
{
num_of_ops1 += ( 1 + Math.abs(sum));
sum = - 1 ;
}
else if (i % 2 == 1 && sum <= 0 )
{
num_of_ops1 += ( 1 + Math.abs(sum));
sum = 1 ;
}
}
sum = 0 ;
for ( int i = 0 ; i < N; i++)
{
sum += a[i];
if (i % 2 == 1 && sum >= 0 )
{
num_of_ops2 += ( 1 + Math.abs(sum));
sum = - 1 ;
}
else if (i % 2 == 0 && sum <= 0 )
{
num_of_ops2 += ( 1 + Math.abs(sum));
sum = 1 ;
}
}
return Math.min(num_of_ops1, num_of_ops2);
}
public static void main(String[] args)
{
int arr[] = { 3 , - 4 , 5 , 0 , 1 };
int N = arr.length;
System.out.print(minOperations(arr, N));
}
}
|
Python3
def minOperations(a, N):
num_of_ops1 = num_of_ops2 = sum = 0 ;
for i in range (N):
sum + = a[i]
if (i % 2 = = 0 and sum > = 0 ):
num_of_ops1 + = ( 1 + abs ( sum ))
sum = - 1
elif (i % 2 = = 1 and sum < = 0 ):
num_of_ops1 + = ( 1 + abs ( sum ))
sum = 1
sum = 0
for i in range (N):
sum + = a[i]
if (i % 2 = = 1 and sum > = 0 ):
num_of_ops2 + = ( 1 + abs ( sum ))
sum = - 1
elif (i % 2 = = 0 and sum < = 0 ):
num_of_ops2 + = ( 1 + abs ( sum ))
sum = 1
return min (num_of_ops1, num_of_ops2)
if __name__ = = "__main__" :
arr = [ 3 , - 4 , 5 , 0 , 1 ]
N = len (arr)
print (minOperations(arr, N))
|
C#
using System;
class GFG{
static int minOperations( int []a, int N)
{
int num_of_ops1, num_of_ops2, sum;
num_of_ops1 = num_of_ops2 = sum = 0;
for ( int i = 0; i < N; i++)
{
sum += a[i];
if (i % 2 == 0 && sum >= 0)
{
num_of_ops1 += (1 + Math.Abs(sum));
sum = -1;
}
else if (i % 2 == 1 && sum <= 0)
{
num_of_ops1 += (1 + Math.Abs(sum));
sum = 1;
}
}
sum = 0;
for ( int i = 0; i < N; i++)
{
sum += a[i];
if (i % 2 == 1 && sum >= 0)
{
num_of_ops2 += (1 + Math.Abs(sum));
sum = -1;
}
else if (i % 2 == 0 && sum <= 0)
{
num_of_ops2 += (1 + Math.Abs(sum));
sum = 1;
}
}
return Math.Min(num_of_ops1, num_of_ops2);
}
public static void Main(String[] args)
{
int []arr = { 3, -4, 5, 0, 1 };
int N = arr.Length;
Console.Write(minOperations(arr, N));
}
}
|
Javascript
<script>
function minOperations(a, N)
{
var num_of_ops1, num_of_ops2, sum;
num_of_ops1 = num_of_ops2 = sum = 0;
for (i = 0; i < N; i++)
{
sum += a[i];
if (i % 2 == 0 && sum >= 0)
{
num_of_ops1 += (1 + Math.abs(sum));
sum = -1;
}
else if (i % 2 == 1 && sum <= 0)
{
num_of_ops1 += (1 + Math.abs(sum));
sum = 1;
}
}
sum = 0;
for (i = 0; i < N; i++)
{
sum += a[i];
if (i % 2 == 1 && sum >= 0)
{
num_of_ops2 += (1 + Math.abs(sum));
sum = -1;
}
else if (i % 2 == 0 && sum <= 0)
{
num_of_ops2 += (1 + Math.abs(sum));
sum = 1;
}
}
return Math.min(num_of_ops1, num_of_ops2);
}
var arr = [ 3, -4, 5, 0, 1 ];
var N = arr.length;
document.write(minOperations(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)