Minimize increments or decrements required to make sum and product of array elements non-zero
Last Updated :
29 Jan, 2022
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:
- Traverse the given array and count the number of zeros in the array.
- Find the sum of the given array.
- If the count of zeros is greater than 0 then the result is that count.
- Else if the sum is equal to 0, then the result is 1.
- Else the result will be 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int array_sum( int arr[], int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
int countOperations( int arr[], int N)
{
int count_zeros = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] == 0)
count_zeros++;
}
int sum = array_sum(arr, N);
if (count_zeros)
return count_zeros;
if (sum == 0)
return 1;
return 0;
}
int main()
{
int arr[] = { -1, -1, 0, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << countOperations(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int array_sum( int arr[],
int n)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += arr[i];
return sum;
}
static int countOperations( int arr[],
int N)
{
int count_zeros = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (arr[i] == 0 )
count_zeros++;
}
int sum = array_sum(arr, N);
if (count_zeros != 0 )
return count_zeros;
if (sum == 0 )
return 1 ;
return 0 ;
}
public static void main(String[] args)
{
int arr[] = {- 1 , - 1 , 0 , 0 };
int N = arr.length;
System.out.print(countOperations(arr, N));
}
}
|
Python3
def array_sum(arr, n):
sum = 0
for i in range (n):
sum + = arr[i]
return sum
def countOperations(arr, N):
count_zeros = 0
for i in range (N):
if (arr[i] = = 0 ):
count_zeros + = 1
sum = array_sum(arr, N)
if (count_zeros):
return count_zeros
if ( sum = = 0 ):
return 1
return 0
if __name__ = = '__main__' :
arr = [ - 1 , - 1 , 0 , 0 ]
N = len (arr)
print (countOperations(arr, N))
|
C#
using System;
class GFG{
static int array_sum( int [] arr,
int n)
{
int sum = 0;
for ( int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
static int countOperations( int [] arr,
int N)
{
int count_zeros = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] == 0)
count_zeros++;
}
int sum = array_sum(arr, N);
if (count_zeros != 0)
return count_zeros;
if (sum == 0)
return 1;
return 0;
}
public static void Main()
{
int [] arr = { -1, -1, 0, 0 };
int N = arr.Length;
Console.Write(countOperations(arr, N));
}
}
|
Javascript
<script>
function array_sum(arr , n) {
var sum = 0;
for (i = 0; i < n; i++)
sum += arr[i];
return sum;
}
function countOperations(arr , N) {
var count_zeros = 0;
for (i = 0; i < N; i++) {
if (arr[i] == 0)
count_zeros++;
}
var sum = array_sum(arr, N);
if (count_zeros != 0)
return count_zeros;
if (sum == 0)
return 1;
return 0;
}
var arr = [ -1, -1, 0, 0 ];
var N = arr.length;
document.write(countOperations(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...