Skip to content
Related Articles

Related Articles

Check whether bitwise AND of N numbers is Even or Odd
  • Last Updated : 16 Mar, 2021
GeeksforGeeks - Summer Carnival Banner

Given an array arr[] containing N numbers. The task is to check whether the bitwise-AND of the given N numbers is even or odd.
Examples
 

Input: arr[] = { 2, 12, 20, 36, 38 } 
Output: Even
Input: arr[] = { 3, 9, 17, 13, 15 } 
Output: Odd 
 

 

A Simple Solution is to first find the AND of the given N numbers, then check if this AND is even or odd.
A Better Solution is based on bit manipulation and Mathematical facts. 
 

  • Bitwise AND of any two even numbers is an even number.
  • Bitwise AND of any two odd numbers is an odd number.
  • Bitwise AND of an even and an odd number is an even number.

Based on the above facts, it can be deduced that if at least one even number is present in the array then the bitwise AND of whole array will be even otherwise odd.
Below is the implementation of the above approach: 
 



C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the bitwise AND
// of the array elements is even or odd
void checkEvenOdd(int arr[], int n)
{
    for (int i = 0; i < n; i++) {
 
        // If at least an even element is present
        // then the bitwise AND of the
        // array elements will be even
        if (arr[i] % 2 == 0) {
 
            cout << "Even";
            return;
        }
    }
 
    cout << "Odd";
}
 
// Driver code
int main()
{
    int arr[] = { 2, 12, 20, 36, 38 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    checkEvenOdd(arr, n);
 
    return 0;
}

Java




// Java implementation of the approach
import java.io.*;
 
class GFG
{
         
// Function to check if the bitwise AND
// of the array elements is even or odd
static void checkEvenOdd(int []arr, int n)
{
    for (int i = 0; i < n; i++)
    {
 
        // If at least an even element is present
        // then the bitwise AND of the
        // array elements will be even
        if (arr[i] % 2 == 0)
        {
            System.out.print("Even");
            return;
        }
    }
    System.out.println("Odd");
}
 
// Driver code
public static void main (String[] args)
{
    int []arr = { 2, 12, 20, 36, 38 };
    int n = arr.length;
     
    checkEvenOdd(arr, n);
}
}
 
// This code is contributed by @tushil

Python3




# Python3 implementation of the approach
 
# Function to check if the bitwise AND
# of the array elements is even or odd
def checkEvenOdd(arr, n) :
 
    for i in range(n) :
 
        # If at least an even element is present
        # then the bitwise AND of the
        # array elements will be even
        if (arr[i] % 2 == 0) :
 
            print("Even", end = "");
            return;
 
    print("Odd", end = "");
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 2, 12, 20, 36, 38 ];
    n = len(arr);
 
    checkEvenOdd(arr, n);
     
# This code is contributed by AnkitRai01

C#




// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to check if the bitwise AND
// of the array elements is even or odd
static void checkEvenOdd(int []arr, int n)
{
    for (int i = 0; i < n; i++)
    {
 
        // If at least an even element is present
        // then the bitwise AND of the
        // array elements will be even
        if (arr[i] % 2 == 0)
        {
            Console.Write ("Even");
            return;
        }
    }
    Console.Write ("Odd");
}
 
// Driver code
static public void Main ()
{
    int []arr = { 2, 12, 20, 36, 38 };
    int n = arr.Length;
 
    checkEvenOdd(arr, n);
}
}
 
// This code is contributed by ajit..

Javascript




<script>
    // Javascript implementation of the approach
     
    // Function to check if the bitwise AND
    // of the array elements is even or odd
    function checkEvenOdd(arr, n)
    {
        for (let i = 0; i < n; i++) 
        {
       
            // If at least an even element is present
            // then the bitwise AND of the
            // array elements will be even
            if (arr[i] % 2 == 0) 
            {
                document.write ("Even");
                return;
            }
        }
        document.write ("Odd");
    }
     
    let arr = [ 2, 12, 20, 36, 38 ];
    let n = arr.length;
   
    checkEvenOdd(arr, n);
     
</script>
Output: 
Even

 

Time Complexity: O(N)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :