Check if all the elements can be made of same parity by inverting adjacent elements | C ++ implementation of the approach ; Function to check if parity can be made same or not ; Iterate and count the parity ; Odd ; Even ; Condition check ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool flipsPossible ( int a [ ] , int n ) { int count_odd = 0 , count_even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] & 1 ) count_odd ++ ; else count_even ++ ; } if ( count_odd % 2 && count_even % 2 ) return false ; else return true ; } int main ( ) { int a [ ] = { 1 , 0 , 1 , 1 , 0 , 1 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( flipsPossible ( a , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }