Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 <bits/stdc++.h>
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 program 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


Javascript




<script>
 
// Javascript 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
function check(arr, n)
{
    let count = 0;
 
    for(let 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
let arr = [ 3, 9, 12, 13, 15 ];
let n = arr.length;
 
// Function call
document.write(check(arr, n));
 
// This code is contributed by subham348
 
</script>


Output: 

Even

 

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



Last Updated : 10 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads