Sum of elements from an array having even parity | Function that returns true if x has even parity ; We basically count set bits https : www . geeksforgeeks . org / count - set - bits - in - an - integer / ; Function to return the sum of the elements from an array which have even parity ; If a [ i ] has even parity ; Driver code
function checkEvenParity ( x ) { let parity = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; parity ++ ; } if ( parity % 2 == 0 ) return true ; else return false ; } function sumlist ( a , n ) { let sum = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( checkEvenParity ( a [ i ] ) ) sum += a [ i ] ; } return sum ; } let arr = [ 2 , 4 , 3 , 5 , 9 ] ; let n = arr . length ; document . write ( sumlist ( arr , n ) ) ;