Check if all the elements can be made of same parity by inverting adjacent elements | Function to check if parity can be made same or not ; Iterate and count the parity ; Odd ; Even ; Condition check ; Drivers code
function flipsPossible ( a , n ) { let count_odd = 0 ; let count_even = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] & 1 ) == 1 ) count_odd ++ ; else count_even ++ ; } if ( count_odd % 2 == 1 && count_even % 2 == 1 ) return false ; else return true ; } let a = [ 1 , 0 , 1 , 1 , 0 , 1 ] ; let n = a . length ; if ( flipsPossible ( a , n ) ) document . write ( " " ) ; else document . write ( " " ) ;