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

• Last Updated : 10 Nov, 2021

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

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 Oddstring 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 Codeint 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 program to check if the XOR// of an array is Even or Oddimport java.util.*; class GFG{ // Function to check if the XOR of// an array of integers is Even or Oddstatic 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 Codepublic 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 Odddef 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 Codeif __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 Oddusing System;using System.Collections.Generic;using System.Linq;  class GFG{ // Function to check if the XOR of// an array of integers is Even or Oddstatic 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

## Javascript



Output:

Even

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up