Open In App

Minimize increments or decrements required to make sum and product of array elements non-zero

Last Updated : 29 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of N integers, the task is to count the minimum number of increment or decrement operations required on the array such that the sum and product of all the elements of the array arr[] are non-zero.

Examples:

Input: arr[] = {-1, -1, 0, 0}
Output: 2
Explanation: Perform the following operations to update the array as:
Operation 1: Incrementing arr[2] modifies array to {-1, -1, 1, 0}.
Operation 2: Decrementing arr[3] modifies array to {-1, -1, 1, -1}.
Therefore, the sum and product of the above array is -2 and -1 which is non-zero.

Input: arr[] = {-2, 1, 0}
Output: 1

Approach: The given problem can be solved based on the following observations:

  • Minimum steps required to make the array product non-zero and for the product to be non-zero all elements must be non-zero.
  • Minimum steps required to make the sum of the array non-zero if the sum is negative then decrement all the 0s elements by 1 and if the sum is positive then increment all the zero elements by 1 and if the sum is non-zero then, simply increment or decrement any element of the array.

Follow the below steps to solve this problem:

  1. Traverse the given array and count the number of zeros in the array.
  2. Find the sum of the given array.
  3. If the count of zeros is greater than 0 then the result is that count.
  4. Else if the sum is equal to 0, then the result is 1.
  5. Else the result will be 0.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the sum of array
int array_sum(int arr[], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += arr[i];
 
    // Return the sum
    return sum;
}
 
// Function that counts the minimum
// operations required to make the
// sum and product of array non-zero
int countOperations(int arr[], int N)
{
    // Stores count of zero elements
    int count_zeros = 0;
 
    // Iterate over the array to
    // count zero elements
    for (int i = 0; i < N; i++) {
        if (arr[i] == 0)
            count_zeros++;
    }
 
    // Sum of elements of the array
    int sum = array_sum(arr, N);
 
    // Print the result
    if (count_zeros)
        return count_zeros;
 
    if (sum == 0)
        return 1;
 
    return 0;
}
 
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { -1, -1, 0, 0 };
 
    // Size of array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << countOperations(arr, N);
 
    return 0;
}


Java




// Java program for the above approach
import java.util.*;
class GFG{
   
// Function to find the
// sum of array
static int array_sum(int arr[],
                     int n)
{
  int sum = 0;
   
  for (int i = 0; i < n; i++)
    sum += arr[i];
 
  // Return the sum
  return sum;
}
 
// Function that counts the minimum
// operations required to make the
// sum and product of array non-zero
static int countOperations(int arr[],
                           int N)
{
  // Stores count of zero
  // elements
  int count_zeros = 0;
 
  // Iterate over the array to
  // count zero elements
  for (int i = 0; i < N; i++)
  {
    if (arr[i] == 0)
      count_zeros++;
  }
 
  // Sum of elements of the
  // array
  int sum = array_sum(arr, N);
 
  // Print the result
  if (count_zeros != 0)
    return count_zeros;
 
  if (sum == 0)
    return 1;
 
  return 0;
}
 
// Driver Code
public static void main(String[] args)
{
  // Given array arr[]
  int arr[] = {-1, -1, 0, 0};
 
  // Size of array
  int N = arr.length;
 
  // Function Call
  System.out.print(countOperations(arr, N));
}
}
 
// This code is contributed by sanjoy_62


Python3




# Python3 program for the
# above approach
 
# Function to find the
# sum of array
def array_sum(arr, n):
   
    sum = 0
     
    for i in range(n):
        sum += arr[i]
         
    # Return the sum
    return sum
 
# Function that counts the minimum
# operations required to make the
# sum and product of array non-zero
def countOperations(arr, N):
   
    # Stores count of zero
    # elements
    count_zeros = 0
 
    # Iterate over the array to
    # count zero elements
    for i in range(N):
        if (arr[i] == 0):
            count_zeros+=1
 
    # Sum of elements of the
    # array
    sum = array_sum(arr, N)
 
    # Print result
    if (count_zeros):
        return count_zeros
 
    if (sum == 0):
        return 1
 
    return 0
 
# Driver Code
if __name__ == '__main__':
   
    # Given array arr[]
    arr = [-1, -1, 0, 0]
 
    # Size of array
    N = len(arr)
 
    # Function Call
    print(countOperations(arr, N))
 
# This code is contributed by Mohit Kumar 29


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the
// sum of array
static int array_sum(int[] arr,
                     int n)
{
  int sum = 0;
   
  for(int i = 0; i < n; i++)
    sum += arr[i];
 
  // Return the sum
  return sum;
}
 
// Function that counts the minimum
// operations required to make the
// sum and product of array non-zero
static int countOperations(int[] arr,
                           int N)
{
   
  // Stores count of zero
  // elements
  int count_zeros = 0;
 
  // Iterate over the array to
  // count zero elements
  for(int i = 0; i < N; i++)
  {
    if (arr[i] == 0)
      count_zeros++;
  }
 
  // Sum of elements of the
  // array
  int sum = array_sum(arr, N);
 
  // Print the result
  if (count_zeros != 0)
    return count_zeros;
 
  if (sum == 0)
    return 1;
 
  return 0;
}
 
// Driver Code
public static void Main()
{
   
  // Given array arr[]
  int[] arr = { -1, -1, 0, 0 };
 
  // Size of array
  int N = arr.Length;
 
  // Function call
  Console.Write(countOperations(arr, N));
}
}
 
// This code is contributed by code_hunt


Javascript




<script>
 
// Javascript program for the above approach
 
    // Function to find the
    // sum of array
    function array_sum(arr , n) {
        var sum = 0;
 
        for (i = 0; i < n; i++)
            sum += arr[i];
 
        // Return the sum
        return sum;
    }
 
    // Function that counts the minimum
    // operations required to make the
    // sum and product of array non-zero
    function countOperations(arr , N) {
        // Stores count of zero
        // elements
        var count_zeros = 0;
 
        // Iterate over the array to
        // count zero elements
        for (i = 0; i < N; i++) {
            if (arr[i] == 0)
                count_zeros++;
        }
 
        // Sum of elements of the
        // array
        var sum = array_sum(arr, N);
 
        // Print the result
        if (count_zeros != 0)
            return count_zeros;
 
        if (sum == 0)
            return 1;
 
        return 0;
    }
 
    // Driver Code
     
        // Given array arr
        var arr = [ -1, -1, 0, 0 ];
 
        // Size of array
        var N = arr.length;
 
        // Function Call
        document.write(countOperations(arr, N));
 
// This code contributed by umadevi9616
 
</script>


Output: 

2

 

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads