Make all elements zero by decreasing any two elements by one at a time | Java implementation of the above 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
public class GFG { static boolean checkZeroArray ( int [ ] arr , int n ) { int sum = 0 , maximum = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + arr [ i ] ; maximum = Math . max ( maximum , arr [ i ] ) ; } if ( sum % 2 == 0 && maximum <= sum / 2 ) return true ; return false ; } 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 " ) ; } }