给定N个整数的数组arr [] ,任务是计算数组上所需的最小递增或递减操作数,以使数组arr []的所有元素的总和与乘积不为零。
例子:
Input: arr[] = {-1, -1, 0, 0}
Output: 2
Explanation: Perform the following operations to update the array as:
Operation 1: Incrementing arr[2] modifies arry 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
方法:可以根据以下观察结果解决给定问题:
- 使数组乘积为非零以及为乘积为非零所需的最少步骤,所有元素都必须为非零。
- 如果总和为负,则使数组总和为非零所需的最少步骤,然后将所有0s元素减1 ,如果总和为正,则将所有零元素增加1 ,如果总和为非零,则,只需增加或减少数组的任何元素。
请按照以下步骤解决此问题:
- 遍历给定的数组并计算数组中的零个数。
- 找到给定数组的总和。
- 如果零计数大于0,则结果为该计数。
- 否则,如果总和等于0,则结果为1 。
- 否则结果将为0 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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)
# Prthe 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
输出:
2
时间复杂度: O(N)
辅助空间: O(1)