📌  相关文章
📜  使和和数组所有元素的乘积非零的最小步骤

📅  最后修改于: 2021-09-06 05:47:01             🧑  作者: Mango

给定一个由N 个整数组成的数组arr ,任务是找到可以使数组中所有元素的和和乘积不为零的最小步长。在一个步骤中,数组的任何元素都可以增加 1。
例子:

方法:这个想法是将问题分成两部分,即——

  1. 使阵列乘积不为零所需的最小步骤。
  2. 使数组总和不等于零所需的最小步骤。

对于不等于零的数组所有元素的乘积,则数组的每个元素都应为非零,并且如果数组总和为零,则要获得不等于零的数组总和将任何元素增加 1。
例如:

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. 

算法:

  • 遍历数组以检查是否存在为零的元素,然后将元素增加 1,并将步数增加 1
  • 再次,迭代数组并找到数组的总和,如果数组的总和为零,则将任何元素增加 1,并将步数增加 1。

下面是上述方法的实现:

C++
// C++ implementation to find the
// minimum steps to make the array sum
// and the product not equal to zero
#include 
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 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 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 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 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


输出:
3

性能分析:

  • 时间复杂度:在给定的方法中,有两次迭代来计算使乘积为非零所需的最小步骤,以及另一次迭代来计算数组的总和。上)
  • 空间复杂度:在给定的方法中,没有使用额外的空间。 O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live