给定一个由正整数组成的数组arr [] ,任务是通过执行以下操作来检查给定数组的所有元素是否可以设为0:
- 选择两个索引i和j ,使i!= j并从arr [i]和arr [j]中减去1
- 以上操作可以执行任意次
例子:
Input: arr[] = {1, 2, 3, 4}
Output: Yes
Explanation:
First, choose values 2 and 4 and perform the above operation 2 times. Then the array becomes 1 0 3 2.
Now choose 1 and 3 and apply above operation once to get 0 0 2 2.
Now pick two 2s and perform the above operation twice.
Finally array becomes 0 0 0 0.
Input: arr[] = {5, 5, 5, 5, 5}
Output: No
方法:仔细观察问题,可以观察到,如果只有1个元素或所有元素的总和为奇数,则不可能将所有元素都设为0。由于在每次迭代中,都会从中减去2因此,只有数组中所有元素之和为偶数时,数组才可以变为0。而且,当数组中的最大数目小于或等于剩余元素的总和时,可以使数组为0。
下面是上述方法的实现:
C++
// C++ program to make the array zero
// by decrementing value in pairs
#include
using namespace std;
// Function to check if all the elements
// can be made 0 in an array
void canMake(int n, int ar[])
{
// Variable to store
// sum and maximum element
// in an array
int sum = 0, maxx = -1;
// Loop to calculate the sum and max value
// of the given array
for (int i = 0; i < n; i++) {
sum += ar[i];
maxx = max(maxx, ar[i]);
}
// If n is 1 or sum is odd or
// sum - max element < max
// then no solution
if (n == 1 || sum % 2 == 1
|| sum - maxx < maxx) {
cout << "No\n";
}
else {
// For the remaining case, print Yes
cout << "Yes\n";
}
}
// Driver code
int main()
{
int n = 6;
int arr[] = { 1, 1, 2, 3, 6, 11 };
canMake(n, arr);
return 0;
}
Java
// Java program to make the array zero
// by decrementing value in pairs
class GFG
{
// Function to check if all the elements
// can be made 0 in an array
static void canMake(int n, int ar[])
{
// Variable to store
// sum and maximum element
// in an array
int sum = 0, maxx = -1;
// Loop to calculate the sum and max value
// of the given array
for (int i = 0; i < n; i++)
{
sum += ar[i];
maxx = Math.max(maxx, ar[i]);
}
// If n is 1 or sum is odd or
// sum - max element < max
// then no solution
if (n == 1 || sum % 2 == 1
|| sum - maxx < maxx)
{
System.out.print("No\n");
}
else
{
// For the remaining case, print Yes
System.out.print("Yes\n");
}
}
// Driver code
public static void main(String[] args)
{
int n = 6;
int arr[] = { 1, 1, 2, 3, 6, 11 };
canMake(n, arr);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to make the array zero
# by decrementing value in pairs
# Function to check if all the elements
# can be made 0 in an array
def canMake(n, ar) :
# Variable to store
# sum and maximum element
# in an array
sum = 0; maxx = -1;
# Loop to calculate the sum and max value
# of the given array
for i in range(n) :
sum += ar[i];
maxx = max(maxx, ar[i]);
# If n is 1 or sum is odd or
# sum - max element < max
# then no solution
if (n == 1 or sum % 2 == 1
or sum - maxx < maxx) :
print("No");
else :
# For the remaining case, print Yes
print("Yes");
# Driver code
if __name__ == "__main__" :
n = 6;
arr = [ 1, 1, 2, 3, 6, 11 ];
canMake(n, arr);
# This code is contributed by AnkitRai01
C#
// C# program to make the array zero
// by decrementing value in pairs
using System;
class GFG
{
// Function to check if all the elements
// can be made 0 in an array
static void canMake(int n, int []ar)
{
// Variable to store
// sum and maximum element
// in an array
int sum = 0, maxx = -1;
// Loop to calculate the sum and max value
// of the given array
for (int i = 0; i < n; i++)
{
sum += ar[i];
maxx = Math.Max(maxx, ar[i]);
}
// If n is 1 or sum is odd or
// sum - max element < max
// then no solution
if (n == 1 || sum % 2 == 1
|| sum - maxx < maxx)
{
Console.Write("No\n");
}
else
{
// For the remaining case, print Yes
Console.Write("Yes\n");
}
}
// Driver code
public static void Main(String[] args)
{
int n = 6;
int []arr = { 1, 1, 2, 3, 6, 11 };
canMake(n, arr);
}
}
// This code is contributed by PrinciRaj1992
输出:
Yes