Find the maximum sum pair in an Array with even parity | Java 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
import java . util . * ; class GFG { static int sz = ( int ) 1e3 ; static boolean isEvenParity ( int x ) { int parity = 0 ; while ( x != 0 ) { if ( x % 2 == 1 ) parity ++ ; x = x >> 1 ; } if ( parity % 2 == 0 ) return true ; else return false ; } static void printArray ( int arr [ ] , int len ) { for ( int i = 0 ; i < len ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } static void findPairEvenParity ( int arr [ ] , int len ) { int firstMaximum = Integer . MIN_VALUE ; int secondMaximum = Integer . MIN_VALUE ; 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 ] ; } } } System . out . print ( firstMaximum + " ▁ " + secondMaximum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 18 , 15 , 8 , 9 , 14 } ; int len = arr . length ; findPairEvenParity ( arr , len ) ; } }