📌  相关文章
📜  检查是否可以通过成对递减值来使给定数组的所有元素都为 0

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

给定一个由正整数组成的数组arr[] ,任务是通过执行以下操作来检查给定数组的所有元素是否都可以为 0:

  • 选择两个索引ij使得i != j并从 arr[i] 和 arr[j] 中减去 1
  • 以上操作可以进行任意次

例子:

方法:仔细观察问题,可以观察到,如果只有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


Javascript


输出:
Yes

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