Minimum steps to make sum and the product of all elements of array non-zero
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++
#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;
}
int steps( int n, int a[])
{
int count_steps = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] == 0)
{
a[i] += 1;
count_steps += 1;
}
}
if ( sum(a, n) != 0)
return count_steps;
else
return count_steps + 1;
}
int main()
{
int n = 4;
int a[] = {-1, -1, 0, 0};
int count = steps(n, a);
cout<<(count);
return 0;
}
|
Java
class GFG
{
static int steps( int n, int []a)
{
int count_steps = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (a[i] == 0 )
{
a[i] += 1 ;
count_steps += 1 ;
}
}
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;
}
public static void main(String []args) {
int n = 4 ;
int []a = {- 1 , - 1 , 0 , 0 };
int count = steps(n, a);
System.out.println(count);
}
}
|
Python
def steps(n, a):
count_steps = 0
for i in range (n):
if a[i] = = 0 :
a[i] + = 1
count_steps + = 1
if sum (a)! = 0 :
return count_steps
else :
return count_steps + 1
if __name__ = = "__main__" :
n = 4
a = [ - 1 , - 1 , 0 , 0 ]
count = steps(n, a)
print (count)
|
C#
using System;
class GFG
{
static int steps( int n, int []a)
{
int count_steps = 0;
for ( int i = 0; i < n; i++)
{
if (a[i] == 0)
{
a[i] += 1;
count_steps += 1;
}
}
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;
}
public static void Main(String []args) {
int n = 4;
int []a = {-1, -1, 0, 0};
int count = steps(n, a);
Console.WriteLine(count);
}
}
|
Javascript
<script>
function steps(n, a)
{
let count_steps = 0;
for (let i = 0; i < n; i++)
{
if (a[i] == 0)
{
a[i] += 1;
count_steps += 1;
}
}
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;
}
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)
Last Updated :
03 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...