# Find the maximum sum pair in an Array with even parity

Given an array arr[] of N integers, the task is to find a pair with even parity and maximum sum.

Examples:

Input: arr[] = {18, 15, 8, 9, 14}
Output: 18 15
Explanation:
Binary Representation of elements –
18 => 10010, Parity = 2
15 => 1111, Parity = 4
8 => 1000, Parity = 1
9 => 1001, Parity = 2
14 => 1110, Parity = 3
Here, parity for elements 18, 15 and 9 are even
Hence, pair with maximum sum will be 18 and 15

Input: arr[] = {5, 3, 4, 2, 9}
Output: 9 5
Explanation:
The array contains three even parity values 5, 3 and 9.
Hence, pair with maximum sum will be 9 and 5.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The key observation in the problem is if we choose two maximum numbers with even parity, then it will be the desired answer.

Traverse the array and check if the current element is having even parity. Then, update the two maximum numbers with even parity.

```if (findParity(arr[i]) % 2 == 0)
if (arr[i] >= firstMaximum)
secondMaximum = firstMaximum
firstMaximum  = arr[i]
elif (arr[i] >= secondMaximum)
secondMaximum = arr[i]
```

Below is the implementation of the above approach:

## C++

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

## Java

 `// Java implementation to find  ` `// a pair with even parity and ` `// maximum sum ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `static` `int` `sz = (``int``) 1e3; ` ` `  `// Function that returns true  ` `// if count of set bits  ` `// in given number is even ` `static` `boolean` `isEvenParity(``int` `x) ` `{ ` `     `  `    ``// Parity will store the ` `    ``// count of set bits ` `    ``int` `parity = ``0``; ` `     `  `    ``while` `(x != ``0``)  ` `    ``{ ` `        ``if` `(x % ``2` `== ``1``) ` `            ``parity++; ` `        ``x = x >> ``1``; ` `    ``} ` ` `  `    ``if` `(parity % ``2` `== ``0``) ` `        ``return` `true``; ` `    ``else` `        ``return` `false``; ` `} ` ` `  `// Function to print the  ` `// elements of the array ` `static` `void` `printArray(``int` `arr[], ` `                       ``int` `len) ` `{ ` `    ``for``(``int` `i = ``0``; i < len; i++) ` `    ``{ ` `       ``System.out.print(arr[i] + ``" "``); ` `    ``} ` `} ` ` `  `// Function to remove all the  ` `// even parity elements from  ` `// the given array ` `static` `void` `findPairEvenParity(``int` `arr[], ` `                               ``int` `len) ` `{ ` `    ``int` `firstMaximum = Integer.MIN_VALUE; ` `    ``int` `secondMaximum = Integer.MIN_VALUE; ` `     `  `    ``// Traverse the array ` `    ``for``(``int` `i = ``0``; i < len; i++) ` `    ``{ ` `        `  `       ``// If the current element  ` `       ``// has even parity ` `       ``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); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `arr[] = { ``18``, ``15``, ``8``, ``9``, ``14` `}; ` `    ``int` `len = arr.length; ` `     `  `    ``// Function Call ` `    ``findPairEvenParity(arr, len); ` `} ` `} ` ` `  `// This code is contributed by amal kumar choubey `

## C#

 `// C# implementation to find  ` `// a pair with even parity and ` `// maximum sum ` `using` `System; ` `class` `GFG{ ` ` `  `static` `int` `sz = (``int``) 1e3; ` ` `  `// Function that returns true  ` `// if count of set bits  ` `// in given number is even ` `static` `bool` `isEvenParity(``int` `x) ` `{ ` `     `  `    ``// Parity will store the ` `    ``// count of set bits ` `    ``int` `parity = 0; ` `     `  `    ``while` `(x != 0)  ` `    ``{ ` `        ``if` `(x % 2 == 1) ` `            ``parity++; ` `        ``x = x >> 1; ` `    ``} ` ` `  `    ``if` `(parity % 2 == 0) ` `        ``return` `true``; ` `    ``else` `        ``return` `false``; ` `} ` ` `  `// Function to print the  ` `// elements of the array ` `static` `void` `printArray(``int` `[]arr, ` `                       ``int` `len) ` `{ ` `    ``for``(``int` `i = 0; i < len; i++) ` `    ``{ ` `        ``Console.Write(arr[i] + ``" "``); ` `    ``} ` `} ` ` `  `// Function to remove all the  ` `// even parity elements from  ` `// the given array ` `static` `void` `findPairEvenParity(``int` `[]arr, ` `                               ``int` `len) ` `{ ` `    ``int` `firstMaximum = Int32.MinValue; ` `    ``int` `secondMaximum = Int32.MinValue; ` `     `  `    ``// Traverse the array ` `    ``for``(``int` `i = 0; i < len; i++) ` `    ``{ ` `         `  `        ``// If the current element  ` `        ``// has even parity ` `        ``if` `(isEvenParity(arr[i])) ` `        ``{ ` `            ``if` `(arr[i] >= firstMaximum) ` `            ``{ ` `                ``secondMaximum = firstMaximum; ` `                ``firstMaximum = arr[i]; ` `            ``} ` `            ``else` `if` `(arr[i] >= secondMaximum) ` `            ``{ ` `                ``secondMaximum = arr[i]; ` `            ``} ` `        ``} ` `    ``} ` `    ``Console.Write(firstMaximum + ``" "` `+ ` `                  ``secondMaximum); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `[]arr = { 18, 15, 8, 9, 14 }; ` `    ``int` `len = arr.Length; ` `     `  `    ``// Function Call ` `    ``findPairEvenParity(arr, len); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech `

Output:

```18 15
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.