Related Articles

Related Articles

Minimize increments or decrements required to make sum and product of array elements non-zero
  • Difficulty Level : Expert
  • Last Updated : 13 Nov, 2020

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 arry 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)
 
    # Prthe 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output: 

2









 

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :