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:



  1. The sum of the elements of the array must be even.
  2. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 

chevron_right


Output:

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29, AnkitRai01

Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.