Open In App

Min cost required to make the Array sum zero consisting of 1 and -1

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:

Below is the implementation for the above approach:




// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum cost
// to make the sum of the array zero.
int minCost(int arr[], int n)
{
 
    if (n % 2 == 1) {
        return -1;
    }
 
    // Variable to store the total
    // no. of +1's.
    int positiveOnes = 0;
 
    // variable to store the total
    // no. of -1's.
    int negativeOnes = 0;
 
    for (int i = 0; i < n; i++) {
        if (arr[i] == 1) {
            positiveOnes++;
        }
        else {
            negativeOnes++;
        }
    }
 
    // Variable to store the total no
    // of operations required to make
    // the sum of array zero.
    int totalOperations;
 
    // Calculate the difference and
    // then divide it by 2.
    totalOperations = abs(positiveOnes - negativeOnes) / 2;
    // Calculate the total cost.
    int ans = totalOperations * 10;
 
    return ans;
}
 
// Drivers code
int main()
{
 
    int n = 6;
    int arr[] = { 1, 1, -1, -1, 1, 1 };
 
    // Function Call
    cout << "Minimum Cost: " << minCost(arr, n);
    return 0;
}




import java.util.Arrays;
 
public class GFG {
 
    // Function to find the minimum cost
    // to make the sum of the array zero.
    static int minCost(int[] arr, int n) {
 
        if (n % 2 == 1) {
            return -1;
        }
 
        // Variable to store the total
        // no. of +1's.
        int positiveOnes = 0;
 
        // variable to store the total
        // no. of -1's.
        int negativeOnes = 0;
 
        for (int i = 0; i < n; i++) {
            if (arr[i] == 1) {
                positiveOnes++;
            } else {
                negativeOnes++;
            }
        }
 
        // Variable to store the total no
        // of operations required to make
        // the sum of array zero.
        int totalOperations;
 
        // Calculate the difference and
        // then divide it by 2.
        totalOperations = Math.abs(positiveOnes - negativeOnes) / 2;
        // Calculate the total cost.
        int ans = totalOperations * 10;
 
        return ans;
    }
 
    // Driver code
    public static void main(String[] args) {
 
        int n = 6;
        int[] arr = { 1, 1, -1, -1, 1, 1 };
 
        // Function Call
        System.out.println("Minimum Cost: " + minCost(arr, n));
    }
}




# Function to find the minimum cost
# to make the sum of the array zero.
 
 
def minCost(arr, n):
 
    if (n % 2 == 1):
        return -1
 
    # Variable to store the total
    # no. of +1's.
    positiveOnes = 0
 
    # variable to store the total
    # no. of -1's.
    negativeOnes = 0
 
    for i in range(n):
        if (arr[i] == 1):
            positiveOnes += 1
        else:
            negativeOnes += 1
 
    # Variable to store the total no
    # of operations required to make
    # the sum of array zero.
    totalOperations = abs(positiveOnes - negativeOnes) // 2
 
    # Calculate the total cost.
    ans = totalOperations * 10
 
    return ans
 
 
# Drivers code
n = 6
arr = [1, 1, -1, -1, 1, 1]
 
# Function Call
print("Minimum Cost:", minCost(arr, n))




using System;
 
public class Program
{
    // Function to find the minimum cost
    // to make the sum of the array zero.
    public static int MinCost(int[] arr, int n)
    {
        if (n % 2 == 1)
        {
            return -1;
        }
        // Variable to store the total
        // no. of +1's.
        int positiveOnes = 0;
        // variable to store the total
        // no. of -1's.
        int negativeOnes = 0;
        for (int i = 0; i < n; i++)
        {
            if (arr[i] == 1)
            {
                positiveOnes++;
            }
            else
            {
                negativeOnes++;
            }
        }
        // Variable to store the total no
        // of operations required to make
        // the sum of array zero.
        int totalOperations;
        // Calculate the difference and
        // then divide it by 2.
        totalOperations = Math.Abs(positiveOnes - negativeOnes) / 2;
        // Calculate the total cost.
        int ans = totalOperations * 10;
        return ans;
    }
    // Drivers code
    public static void Main()
    {
        int n = 6;
        int[] arr = { 1, 1, -1, -1, 1, 1 };
        // Function Call
        Console.WriteLine("Minimum Cost: " + MinCost(arr, n));
    }
}




function GFG(arr) {
  const n = arr.length;
  if (n % 2 === 1) {
    return -1;
  }
  // Variable to store the total
  // no. of +1's.
  let positiveOnes = 0;
  // Variable to store the total no. of -1's.
  let negativeOnes = 0;
  for (let i = 0; i < n; i++) {
    if (arr[i] === 1) {
      positiveOnes++;
    } else {
      negativeOnes++;
    }
  }
  let totalOperations;
  // Calculate the difference and then divide it by 2.
  totalOperations = Math.abs(positiveOnes - negativeOnes) / 2;
  // Calculate the total cost.
  let ans = totalOperations * 10;
  return ans;
}
// Main function
function main() {
  const arr = [1, 1, -1, -1, 1, 1];
  // Function Call
  const result = GFG(arr);
  console.log("Minimum Cost:", result);
}
main();

Output
Minimum Cost: 10





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


Article Tags :