📌  相关文章
📜  通过一次将任意两个元素减一来使所有元素为零

📅  最后修改于: 2021-04-22 10:31:55             🧑  作者: Mango

给定数组arr [] ,任务是检查是否可以通过给定的操作使数组的所有元素为零。在单个操作中,任意两个元素arr [i]arr [j]可以同时减一。

例子:

方法:如果给定数组满足以下条件,则只能将其设为零:

  1. 数组元素的总和必须是偶数。
  2. 最大元素必须小于或等于⌊sum/2⌋ ,其中sum是其他元素的总和。

下面是上述方法的实现:

CPP
// C++ implementation of the approach
#include 
using namespace std;
  
// Function that returns true if all
// the array elements can be made
// 0 with the given operation
bool checkZeroArray(int* arr, int n)
{
  
    // Find the maximum element
    // and the sum
    int sum = 0, maximum = INT_MIN;
    for (int i = 0; i < n; i++) {
        sum = sum + arr[i];
        maximum = max(maximum, arr[i]);
    }
  
    // Check the required condition
    if (sum % 2 == 0 && maximum <= sum / 2)
        return true;
  
    return false;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 1, 2, 2 };
    int n = sizeof(arr) / sizeof(int);
  
    if (checkZeroArray(arr, n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the above approach 
public class GFG 
{
      
    // Function that returns true if all 
    // the array elements can be made 
    // 0 with the given operation 
    static boolean checkZeroArray(int []arr, int n) 
    { 
      
        // Find the maximum element 
        // and the sum 
        int sum = 0, maximum = Integer.MIN_VALUE; 
          
        for (int i = 0; i < n; i++) 
        { 
            sum = sum + arr[i]; 
            maximum = Math.max(maximum, arr[i]); 
        } 
      
        // Check the required condition 
        if (sum % 2 == 0 && maximum <= sum / 2) 
            return true; 
      
        return false; 
    } 
      
    // Driver code 
    public static void main (String[] args)
    { 
        int arr[] = { 1, 2, 1, 2, 2 }; 
        int n = arr.length; 
      
        if (checkZeroArray(arr, n) == true) 
            System.out.println("Yes"); 
        else
            System.out.println("No"); 
    } 
}
  
// This code is contributed by AnkitRai01


Python
# Python3 implementation of the approach
  
# Function that returns true if all
# the array elements can be made
# 0 with the given operation
def checkZeroArray(arr,n):
  
    # Find the maximum element
    # and the sum
    sum = 0
    maximum = -10**9
    for i in range(n):
        sum = sum + arr[i]
        maximum = max(maximum, arr[i])
  
    # Check the required condition
    if (sum % 2 == 0 and maximum <= sum // 2):
        return True
  
    return False
  
# Driver code
  
arr = [1, 2, 1, 2, 2]
n = len(arr)
  
if (checkZeroArray(arr, n)):
    print("Yes")
else:
    print("No")
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the above approach 
using System;
  
class GFG 
{ 
      
    // Function that returns true if all 
    // the array elements can be made 
    // 0 with the given operation 
    static bool checkZeroArray(int []arr, int n) 
    { 
      
        // Find the maximum element 
        // and the sum 
        int sum = 0, maximum = int.MinValue; 
          
        for (int i = 0; i < n; i++) 
        { 
            sum = sum + arr[i]; 
            maximum = Math.Max(maximum, arr[i]); 
        } 
      
        // Check the required condition 
        if (sum % 2 == 0 && maximum <= sum / 2) 
            return true; 
      
        return false; 
    } 
      
    // Driver code 
    public static void Main () 
    { 
        int []arr = { 1, 2, 1, 2, 2 }; 
        int n = arr.Length; 
      
        if (checkZeroArray(arr, n) == true) 
            Console.WriteLine("Yes"); 
        else
            Console.WriteLine("No"); 
    } 
} 
  
// This code is contributed by AnkitRai01


输出:
Yes