Minimum steps to make sum and the product of all elements of array non-zero
Last Updated :
03 Apr, 2023
Given an array arr of N integers, the task is to find the minimum steps in which the sum and product of all elements of the array can be made non-zero. In one step any element of the array can be incremented by 1.
Examples:
Input: N = 4, arr[] = {0, 1, 2, 3}
Output: 1
Explanation:
As product of all elements of the array is zero
Increment the array element 0 by 1, such that array sum and product is not equal to zero.
Input: N = 4, arr[] = {-1, -1, 0, 0}
Output: 3
Explanation:
As product of all elements of the array is zero
Increment the array element 2 and 3 by 1, such that array sum and product is not equal to zero
Approach: The idea is to break problem into two parts that is -
- Minimum steps required to make the array product not equal to zero.
- Minimum steps required to make the array sum not equal to zero.
For the product of all elements of the array not equal to zero, then every element of the array should be non-zero and to get the array sum not equal to zero increment any element by 1 if the array sum is zero.
For Example:
N = 4, arr[] = {0, 1, 2, 3}
Iterate over the array to find,
If there is an element that is zero.
If yes, then increment it by 1 and also
increment the number of steps by 1.
Again, Iterate over the updated array,
To check if the array sum is zero.
If the array sum of the updated array
is zero then increment any element by 1.
Algorithm:
- Iterate over the array to check if there is an element that is zero, then increment the element by 1 and also increment the number of steps by 1
- Again, Iterate over the array and find the sum of the array if the sum of the array is zero then increment any element by 1 and also increment the number of steps by 1.
Below is the implementation of the above approach:
C++
// C++ implementation to find the
// minimum steps to make the array sum
// and the product not equal to zero
#include <bits/stdc++.h>
using namespace std;
int sum(int arr[], int n)
{
int sum = 0;
for(int i= 0; i < n; i++)
sum += arr[i];
return sum;
}
// Function to find the
// minimum steps to make the array sum
// and the product not equal to zero
int steps(int n, int a[])
{
// Variable to store the minimum
// number of steps required
int count_steps = 0;
// Loop to iterate over the array to
// find if there is any element in
// array which is zero
for(int i = 0; i < n; i++)
{
if(a[i] == 0)
{
a[i] += 1;
count_steps += 1;
}
}
// Condition to check if the sum
// of array elements is zero
if( sum(a, n) != 0)
return count_steps;
else
return count_steps + 1;
}
// Driver code
int main()
{
int n = 4;
int a[] = {-1, -1, 0, 0};
int count = steps(n, a);
cout<<(count);
return 0;
}
// This code is contributed by Rajput-Ji
Java
// Java implementation to find the
// minimum steps to make the array sum
// and the product not equal to zero
class GFG
{
// Function to find the
// minimum steps to make the array sum
// and the product not equal to zero
static int steps(int n, int []a)
{
// Variable to store the minimum
// number of steps required
int count_steps = 0;
// Loop to iterate over the array to
// find if there is any element in
// array which is zero
for(int i = 0; i < n; i++)
{
if(a[i] == 0)
{
a[i] += 1;
count_steps += 1;
}
}
// Condition to check if the sum
// of array elements is zero
if( sum(a) != 0)
return count_steps;
else
return count_steps + 1;
}
static int sum(int[] arr)
{
int sum = 0;
for(int i= 0; i < arr.length; i++)
sum += arr[i];
return sum;
}
// Driver code
public static void main(String []args) {
int n = 4;
int []a = {-1, -1, 0, 0};
int count = steps(n, a);
System.out.println(count);
}
}
// This code is contributed by Rajput-Ji
Python
# Python implementation to find the
# minimum steps to make the array sum
# and the product not equal to zero
# Function to find the
# minimum steps to make the array sum
# and the product not equal to zero
def steps(n, a):
# Variable to store the minimum
# number of steps required
count_steps = 0
# Loop to iterate over the array to
# find if there is any element in
# array which is zero
for i in range(n):
if a[i]== 0:
a[i] += 1
count_steps += 1
# Condition to check if the sum
# of array elements is zero
if sum(a)!= 0:
return count_steps
else:
return count_steps + 1
# Driver code
if __name__ == "__main__":
n = 4
a = [-1, -1, 0, 0]
count = steps(n, a)
print(count)
C#
// C# implementation to find the
// minimum steps to make the array sum
// and the product not equal to zero
using System;
class GFG
{
// Function to find the
// minimum steps to make the array sum
// and the product not equal to zero
static int steps(int n, int []a)
{
// Variable to store the minimum
// number of steps required
int count_steps = 0;
// Loop to iterate over the array to
// find if there is any element in
// array which is zero
for(int i = 0; i < n; i++)
{
if(a[i] == 0)
{
a[i] += 1;
count_steps += 1;
}
}
// Condition to check if the sum
// of array elements is zero
if( sum(a) != 0)
return count_steps;
else
return count_steps + 1;
}
static int sum(int[] arr)
{
int sum = 0;
for(int i= 0; i < arr.Length; i++)
sum += arr[i];
return sum;
}
// Driver code
public static void Main(String []args) {
int n = 4;
int []a = {-1, -1, 0, 0};
int count = steps(n, a);
Console.WriteLine(count);
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// Javascript implementation to find the
// minimum steps to make the array sum
// and the product not equal to zero
// Function to find the
// minimum steps to make the array sum
// and the product not equal to zero
function steps(n, a)
{
// Variable to store the minimum
// number of steps required
let count_steps = 0;
// Loop to iterate over the array to
// find if there is any element in
// array which is zero
for(let i = 0; i < n; i++)
{
if(a[i] == 0)
{
a[i] += 1;
count_steps += 1;
}
}
// Condition to check if the sum
// of array elements is zero
if( sum(a) != 0)
return count_steps;
else
return count_steps + 1;
}
function sum(arr)
{
let sum = 0;
for(let i= 0; i < arr.length; i++)
sum += arr[i];
return sum;
}
// Driver Code
let n = 4;
let a = [-1, -1, 0, 0];
let count = steps(n, a);
document.write(count);
</script>
Performance Analysis:
- Time Complexity: In the given approach, there are two iterations to compute the minimum steps required to make the product to non-zero and another iteration to compute the sum of the array. O(N)
- Space Complexity: In the given approach, there is no extra space used. O(1)
Similar Reads
Minimum steps to make the product of the array equal to 1 Given an array arr[] containing N integers. In one step, any element of the array can either be increased or decreased by one. The task is to find minimum steps required such that the product of the array elements becomes 1. Examples: Input: arr[] = { -2, 4, 0 } Output: 5 We can change -2 to -1, 0 t
10 min read
Minimize increments or decrements required to make sum and product of array elements non-zero 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: 2Explanation: Perform the following
6 min read
Minimum element whose n-th power is greater than product of an array of size n Given an array of n integers. Find minimum x which is to be assigned to every array element such that product of all elements of this new array is strictly greater than product of all elements of the initial array. Examples: Input: 4 2 1 10 6 Output: 4 Explanation: Product of elements of initialarra
8 min read
Minimum sum of product of elements of pairs of the given array Given an array arr[] of even number of element N in it. The task is to form N/2 pairs such that sum of product of elements in those pairs is minimum. Examples Input: arr[] = { 1, 6, 3, 1, 7, 8 } Output: 270 Explanation: The pair formed are {1, 1}, {3, 6}, {7, 8} Product of sum of these pairs = 2 * 9
5 min read
Construct array with sum of product of same indexed elements in the given array equal to zero Given an array, arr[] of size N (always even), the task is to construct a new array consisting of N non-zero integers such that the sum of the product of the same indexed elements of arr[] and the new array is equal to 0. If multiple solutions exist, print any one of them. Examples: Input: arr[] = {
6 min read