Make all elements zero by decreasing any two elements by one at a time | C ++ implementation of the approach ; Function that returns true if all the array elements can be made 0 with the given operation ; Find the maximum element and the sum ; Check the required condition ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkZeroArray ( int * arr , int n ) { int sum = 0 , maximum = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + arr [ i ] ; maximum = max ( maximum , arr [ i ] ) ; } if ( sum % 2 == 0 && maximum <= sum / 2 ) return true ; return false ; } 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 ; }