Make all elements zero by decreasing any two elements by one at a time

Given an array arr[], the task is to check whether it is possible to make all the elements of the array zero by the given operation. In a single operation, any two elements arr[i] and arr[j] can be decremented by one at the same time.Â
Examples:Â
Â
Input: arr[] = {1, 2, 1, 2, 2}Â
Output: YesÂ
Decrement the 1st and the 2nd element, arr[] = {0, 1, 1, 2, 2}Â
Decrement the 2nd and the 3rd element, arr[] = {0, 0, 0, 2, 2}Â
Decrement the 4th and the 5th element, arr[] = {0, 0, 0, 1, 1}Â
Decrement the 4th and the 5th element, arr[] = {0, 0, 0, 0, 0}
Input: arr[] = {1, 2, 3, 4, 5}Â
Output: NoÂ
Â
Â
Approach: The given array can be only be made zero if it holds the following conditions true:Â
Â
- The sum of the elements of the array must be even.
- The largest elements must be less than or equal to ?sum / 2? where sum is the sum of the other elements.
Below is the implementation of the above approach:Â
Â
CPP
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function that returns true if all// the array elements can be made// 0 with the given operationbool 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 codeint 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 operationdef 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 |
Javascript
<script>Â
// JavaScript implementation of the above approach          // Function that returns true if all     // the array elements can be made     // 0 with the given operation     function checkZeroArray(arr,n)    {        // Find the maximum element         // and the sum         let sum = 0, maximum = Number.MIN_VALUE;                    for (let 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     let arr=[1, 2, 1, 2, 2 ];    let n = arr.length;     if (checkZeroArray(arr, n) == true)         document.write("Yes");     else        document.write("No");        Â
Â
// This code is contributed by unknown2108Â
</script> |
Yes
Â
Time Complexity: O(n)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



