Min cost required to make the Array sum zero consisting of 1 and -1
Last Updated :
11 Oct, 2023
Given an array arr[] of size N containing only +1 and -1, the task is to make the sum of the array zero with the given operation. In a single operation, any element can be updated as arr[i] = arr[i] * (-1). Each operation cost 10 units. the task is to print the minimum cost required to make the sum of the array zero. If the sum cannot be made zero, then print -1.
Examples:
Input: N = 6, arr[] = {1, 1, -1, -1, 1, 1}
Output: 10
Explanation: The sum of the array is 2, if we perform the given operation one time at any index with value +1, i.e., index: 0, 1, 4, 5
(0-based indexing), the sum of array becomes 0.
Input: N = 5, arr[] = {1, 1, -1, -1, 1}
Output: -1
Explanation: The sum of the array cannot be made zero by applying any number of operations.
Approach: To solve the problem follow the below idea:
As to make the sum of array 0, there must be equal number of +1’s and -1’s. So, we first need to check if the N is odd or even, if the N is odd then number of +1’s and -1’s can never be equal so we can print -1 directly. Now create two variables positiveOnes and negativeOnes to store the number of +1’s and -1’s respectively. Iterate over the array once and store the count for +1’s and -1’s. Now to get the minimum operations, calculate the absolute difference between positiveOnes and negativeOnes and divide it by two. Multiply it by 10 to get the total cost.
Below are the steps for the above approach:
- Check whether the size of the array is odd. If it is odd, it returns -1.
- Initialize two variables positiveOnes and negativeOnes to store the total number of +1’s and -1’s in the array.
- Iterate the array and check if arr[i] == 1, increment the counter variable positiveOnes else increment the counter variable negativeOnes.
- Calculate the absolute difference between positiveOnes and negativeOnes and divides it by 2 to get the number of operations required to make the sum of the array zero.
- Multiply the number of operations by 10 to get the total cost.
- Return the total cost.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minCost( int arr[], int n)
{
if (n % 2 == 1) {
return -1;
}
int positiveOnes = 0;
int negativeOnes = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] == 1) {
positiveOnes++;
}
else {
negativeOnes++;
}
}
int totalOperations;
totalOperations = abs (positiveOnes - negativeOnes) / 2;
int ans = totalOperations * 10;
return ans;
}
int main()
{
int n = 6;
int arr[] = { 1, 1, -1, -1, 1, 1 };
cout << "Minimum Cost: " << minCost(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
public class GFG {
static int minCost( int [] arr, int n) {
if (n % 2 == 1 ) {
return - 1 ;
}
int positiveOnes = 0 ;
int negativeOnes = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] == 1 ) {
positiveOnes++;
} else {
negativeOnes++;
}
}
int totalOperations;
totalOperations = Math.abs(positiveOnes - negativeOnes) / 2 ;
int ans = totalOperations * 10 ;
return ans;
}
public static void main(String[] args) {
int n = 6 ;
int [] arr = { 1 , 1 , - 1 , - 1 , 1 , 1 };
System.out.println( "Minimum Cost: " + minCost(arr, n));
}
}
|
Python3
def minCost(arr, n):
if (n % 2 = = 1 ):
return - 1
positiveOnes = 0
negativeOnes = 0
for i in range (n):
if (arr[i] = = 1 ):
positiveOnes + = 1
else :
negativeOnes + = 1
totalOperations = abs (positiveOnes - negativeOnes) / / 2
ans = totalOperations * 10
return ans
n = 6
arr = [ 1 , 1 , - 1 , - 1 , 1 , 1 ]
print ( "Minimum Cost:" , minCost(arr, n))
|
C#
using System;
public class Program
{
public static int MinCost( int [] arr, int n)
{
if (n % 2 == 1)
{
return -1;
}
int positiveOnes = 0;
int negativeOnes = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] == 1)
{
positiveOnes++;
}
else
{
negativeOnes++;
}
}
int totalOperations;
totalOperations = Math.Abs(positiveOnes - negativeOnes) / 2;
int ans = totalOperations * 10;
return ans;
}
public static void Main()
{
int n = 6;
int [] arr = { 1, 1, -1, -1, 1, 1 };
Console.WriteLine( "Minimum Cost: " + MinCost(arr, n));
}
}
|
Javascript
function GFG(arr) {
const n = arr.length;
if (n % 2 === 1) {
return -1;
}
let positiveOnes = 0;
let negativeOnes = 0;
for (let i = 0; i < n; i++) {
if (arr[i] === 1) {
positiveOnes++;
} else {
negativeOnes++;
}
}
let totalOperations;
totalOperations = Math.abs(positiveOnes - negativeOnes) / 2;
let ans = totalOperations * 10;
return ans;
}
function main() {
const arr = [1, 1, -1, -1, 1, 1];
const result = GFG(arr);
console.log( "Minimum Cost:" , result);
}
main();
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...