# Min price required to make the Array sum 0 consisting of one and -1

Given an array arr[] of measurement N containing simplest +1 and -1, the duty is to make the sum of the array 0 with the given operation. In one operation, any part may also be up to date as arr[i] = arr[i] * (-1). Every operation price 10 devices. the duty is to print the minimal price required to make the sum of the array 0. If the sum can’t be made 0, then print -1.

Examples:

Enter: N = 6, arr[] = {1, 1, -1, -1, 1, 1}
Output: 10
Clarification: The sum of the array is two, if we carry out the given operation one time at any index with price +1, i.e., index: 0, 1, 4, 5Â
(0-based indexing), the sum of array turns into 0.

Enter: N = 5, arr[] = {1, 1, -1, -1, 1}
Output: -1
Clarification: The sum of the array can’t be made 0 by way of making use of any collection of operations.

Way: To unravel the issue practice the underneath concept:

As to make the sum of array 0, there should be equivalent collection of +1âs and -1âs. So, we first wish to take a look at if the N is extraordinary and even, if the N is extraordinary then collection of +1âs and -1âs can by no means be equivalent so we will print -1 without delay. Now create two variables positiveOnes and negativeOnes to retailer the collection of +1âs and -1âs respectively. Iterate over the array as soon as and retailer the depend for +1âs and -1âs. Now to get the minimal operations, calculate absolutely the distinction between positiveOnes and negativeOnes and divide it by way of two. Multiply it by way of 10 to get the entire price.

Beneath are the stairs for the above method:

• Take a look at whether or not the dimensions of the array is extraordinary. Whether it is extraordinary, it returns -1.
• Initialize two variables positiveOnes and negativeOnes to retailer the entire collection of +1âs and -1âs within the array.
• Iterate the array and take a look at if arr[i] == 1, increment the counter variable positiveOnes else increment the counter variable negativeOnes.
• Calculate absolutely the distinction between positiveOnes and negativeOnes and divides it by way of 2 to get the collection of operations required to make the sum of the array 0.
• Multiply the collection of operations by way of 10 to get the entire price.
• Go back the entire price.

Beneath is the implementation for the above method:

## C++

 `#come with ` `the usage of` `namespace` `std;` `Â `Â  `int` `minCost(``int` `arr[], ``int` `n)` `{` `Â `Â  `Â Â Â Â ``if` `(n % 2 == 1) {` `Â Â Â Â Â Â Â Â ``go back` `-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;` `Â `Â  `Â Â Â Â ``go back` `ans;` `}` `Â `Â  `int` `primary()` `{` `Â `Â  `Â Â Â Â ``int` `n = 6;` `Â Â Â Â ``int` `arr[] = { 1, 1, -1, -1, 1, 1 };` `Â `Â  `Â Â Â Â ` `Â Â Â Â ``cout << ``"Minimal Value: "` `<< minCost(arr, n);` `Â Â Â Â ``go back` `0;` `}`

Time Complexity: O(n)
Auxiliary Area: O(n)