Reduce a given Binary Array to a single element by removal of Triplets | Java program to implement the above approach ; Function to check if it is possible to reduce the array to a single element ; Stores frequency of 0 's ; Stores frequency of 1 's ; Condition for array to be reduced ; Otherwise ; Driver Code
class GFG { static void solve ( int arr [ ] , int n ) { int countzeroes = 0 ; int countones = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) countzeroes ++ ; else countones ++ ; } if ( Math . abs ( countzeroes - countones ) == 1 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 0 , 0 , 1 , 1 , 1 } ; int n = arr . length ; solve ( arr , n ) ; } }