Find the maximum sum pair in an Array with even parity | C ++ implementation to find a pair with even parity and maximum sum ; Function that returns true if count of set bits in given number is even ; Parity will store the count of set bits ; Function to print the elements of the array ; Function to remove all the even parity elements from the given array ; Traverse the array ; If the current element has even parity ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int sz = 1e3 ; bool isEvenParity ( int x ) { int parity = 0 ; while ( x != 0 ) { if ( x & 1 ) parity ++ ; x = x >> 1 ; } if ( parity % 2 == 0 ) return true ; else return false ; } void printArray ( int arr [ ] , int len ) { for ( int i = 0 ; i < len ; i ++ ) { cout << arr [ i ] << ' ▁ ' ; } } void findPairEvenParity ( int arr [ ] , int len ) { int firstMaximum = INT_MIN ; int secondMaximum = INT_MIN ; for ( int i = 0 ; i < len ; i ++ ) { if ( isEvenParity ( arr [ i ] ) ) { if ( arr [ i ] >= firstMaximum ) { secondMaximum = firstMaximum ; firstMaximum = arr [ i ] ; } else if ( arr [ i ] >= secondMaximum ) { secondMaximum = arr [ i ] ; } } } cout << firstMaximum << " ▁ " << secondMaximum ; } int main ( ) { int arr [ ] = { 18 , 15 , 8 , 9 , 14 } ; int len = sizeof ( arr ) / sizeof ( int ) ; findPairEvenParity ( arr , len ) ; return 0 ; }