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++
// 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>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimize subarray increments/decrements required to reduce all array elements to 0 Given an array arr[], select any subarray and apply any one of the below operations on each element of the subarray: Increment by oneDecrement by one The task is to print the minimum number of above-mentioned increment/decrement operations required to reduce all array elements to 0. Examples: Input:
5 min read
Minimum increments or decrements required to signs of prefix sum array elements alternating Given an array arr[] of N integers, the task is to find the minimum number of increments/decrements of array elements by 1 to make the sign of prefix sum of array alternating. Examples: Input: arr[] = {1, -3, 1, 0}Output: 4Explanation:Following are the operations performed on the given array element
8 min read
Minimize moves required to make array elements equal by incrementing and decrementing pairs | Set 2 Given an array arr[] of size N, the task is to print the minimum number of moves required to make all array elements equal by selecting any pair of distinct indices and then increment the element at the first index and decrement the element at the other index by 1 each time. If it is impossible to m
7 min read
Minimum increments or decrements by 1 required to make all array elements in AP Given an array arr[] consisting of N integers, the task is to find the minimum number of increment/decrement by 1 required to be performed on array elements to make all the elements of the given array arr[] in AP. If it is not possible to make the array in AP, then print "-1". Examples: Input: arr[]
11 min read
Minimize count of increments of each element of subarrays required to make Array non-increasing Given an array arr[] consisting of N integers, the task is to find the minimum number of operations, which involves incrementing all elements of a subarray by 1, required to make the array non-increasing.Examples: Input: arr[] = {1, 3, 4, 1, 2} Output: 4 Explanation: In operation 1: Choose the subar
5 min read