# Check if the XOR of an array of integers is Even or Odd

Given an array arr containing integers of size N, the task is to check if the XOR of this array is even or odd

Examples:

Input: arr[] = { 2, 4, 7}
Output: Odd
Explanation:
XOR of array = 2 ^ 4 ^ 7 = 1, which is odd

Input: arr[] = { 3, 9, 12, 13, 15 }
Output: Even

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

Naive Solution: First find the XOR of the given array of integers, and then check if this XOR is even or odd.

Time Complexity: O(N)

Efficient Solution: A better Solution is based on bit manipulation fact, that:

• Bitwise XOR of any two even or any two odd numbers is always even.
• Bitwise XOR of an even and an odd number is always odd.

Therefore if the count of odd numbers in the array is odd, then the final XOR will be odd and if it is even, then final XOR will be even.

Below is the implementation of the above approach:

## C++

 `// C++ program to check if the XOR ` `// of an array is Even or Odd ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to check if the XOR of ` `// an array of integers is Even or Odd ` `string check(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `count = 0; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// Count the number ` `        ``// of odd elements ` `        ``if` `(arr[i] & 1) ` `            ``count++; ` `    ``} ` ` `  `    ``// If count of odd elements ` `    ``// is odd, then XOR will be odd ` `    ``if` `(count & 1) ` `        ``return` `"Odd"``; ` ` `  `    ``// Else even ` `    ``else` `        ``return` `"Even"``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 3, 9, 12, 13, 15 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` ` `  `    ``// Function call ` `    ``cout << check(arr, n) << endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java rogram to check if the XOR ` `// of an array is Even or Odd ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// Function to check if the XOR of ` `// an array of integers is Even or Odd ` `static` `String check(``int` `[]arr, ``int` `n) ` `{ ` `    ``int` `count = ``0``; ` ` `  `    ``for` `(``int` `i = ``0``; i < n; i++) { ` ` `  `        ``// Count the number ` `        ``// of odd elements ` `        ``if` `((arr[i] & ``1``)!=``0``) ` `            ``count++; ` `    ``} ` ` `  `    ``// If count of odd elements ` `    ``// is odd, then XOR will be odd ` `    ``if` `((count & ``1``)!=``0``) ` `        ``return` `"Odd"``; ` ` `  `    ``// Else even ` `    ``else` `        ``return` `"Even"``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `[]arr = { ``3``, ``9``, ``12``, ``13``, ``15` `}; ` `    ``int` `n = arr.length; ` ` `  `    ``// Function call ` `    ``System.out.println(check(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by Surendra_Gangwar `

## Python3

 `# Python3 program to check if the XOR ` `# of an array is Even or Odd ` ` `  `# Function to check if the XOR of ` `# an array of integers is Even or Odd ` `def` `check(arr, n): ` `    ``count ``=` `0``; ` ` `  `    ``for` `i ``in` `range``(n): ` ` `  `        ``# Count the number ` `        ``# of odd elements ` `        ``if` `(arr[i] & ``1``): ` `            ``count ``=` `count ``+` `1``; ` `     `  `    ``# If count of odd elements ` `    ``# is odd, then XOR will be odd ` `    ``if` `(count & ``1``): ` `        ``return` `"Odd"``; ` ` `  `    ``# Else even ` `    ``else``: ` `        ``return` `"Even"``; ` ` `  `# Driver Code ` `if` `__name__``=``=``'__main__'``:  ` ` `  `    ``arr ``=` `[ ``3``, ``9``, ``12``, ``13``, ``15` `] ` `    ``n ``=` `len``(arr) ` ` `  `    ``# Function call ` `    ``print``(check(arr, n)) ` ` `  ` `  `# This code is contributed by Princi Singh `

## C#

 `// C# program to check if the XOR ` `// of an array is Even or Odd ` `using` `System; ` `using` `System.Collections.Generic; ` `using` `System.Linq; ` `  `  `class` `GFG  ` `{ ` ` `  `// Function to check if the XOR of ` `// an array of integers is Even or Odd ` `static` `String check(``int` `[]arr, ``int` `n) ` `{ ` `    ``int` `count = 0; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// Count the number ` `        ``// of odd elements ` `        ``if` `(arr[i] == 1) ` `            ``count++; ` `    ``} ` ` `  `    ``// If count of odd elements ` `    ``// is odd, then XOR will be odd ` `    ``if` `(count == 1) ` `        ``return` `"Odd"``; ` ` `  `    ``// Else even ` `    ``else` `        ``return` `"Even"``; ` `} ` ` `  `// Driver Code ` `    ``public` `static` `void` `Main(String[] args)  ` `    ``{ ` `    ``int` `[]arr= { 3, 9, 12, 13, 15 }; ` `    ``int` `n = arr.Length;  ` ` `  `    ``// Function call ` `    ``Console.Write(check(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by shivanisinghss2110 `

Output:

```Even
```

Time Complexity: O(N)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

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.