Reduce a given Binary Array to a single element by removal of Triplets | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; 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 ( abs ( countzeroes - countones ) == 1 ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int arr [ ] = { 0 , 1 , 0 , 0 , 1 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; solve ( arr , n ) ; return 0 ; }