Check whether bitwise AND of N numbers is Even or Odd

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: EvenInput: 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.
C++
// C++ implementation to check whether// bitwise and of n numbers is even or odd#include <bits/stdc++.h>using namespace std;// Function to check if bitwise and// of n numbers is even or oddbool check(int arr[], int n){ int x=arr[0];// assigining the biswise and of given array to x. for(int i=1;i<n;i++){ x=x & arr[i]; } if(x%2==0)// checking is even or not. return false;// if x is even then returning false. return true;// if x is odd returning true.}// Driver Codeint main(){ int arr[] ={ 2, 12, 20, 36, 38}; int n = sizeof(arr) / sizeof(arr[0]); if (check(arr, n)) cout << "Odd"; else cout << "Even"; return 0;} |
Java
import java.util.*;public class Main { // Function to check if bitwise and // of n numbers is even or odd static boolean check(int arr[], int n) { int x = arr[0]; // assigining the biswise and of // given array to x. for (int i = 1; i < n; i++) { x = x & arr[i]; } if (x % 2 == 0) // checking is even or not. return false; // if x is even then returning // false. return true; // if x is odd returning true. } // Driver Code public static void main(String[] args) { int arr[] = { 2, 12, 20, 36, 38 }; int n = arr.length; if (check(arr, n)) System.out.println("Odd"); else System.out.println("Even"); }} |
Python3
# Function to check if bitwise and# of n numbers is even or odddef check(arr, n): x = arr[0] for i in range(1, n): x = x & arr[i] if x % 2 == 0: return False return True# Driver Codeif __name__ == '__main__': arr = [2, 12, 20, 36, 38] n = len(arr) if check(arr, n): print("Odd") else: print("Even") |
Javascript
// JavaScript implementation to check whether// bitwise and of n numbers is even or odd// Function to check if bitwise and// of n numbers is even or oddfunction check(arr, n) { let x = arr[0]; // assigining the biswise and of given array to x. for (let i = 1; i < n; i++) { x = x & arr[i]; } if (x % 2 == 0) // checking is even or not. return false; // if x is even then returning false. return true; // if x is odd returning true.}// Driver Codelet arr = [2, 12, 20, 36, 38];let n = arr.length;if (check(arr, n)) console.log("Odd");else console.log("Even"); |
C#
using System;public class Program{ // Function to check if bitwise and // of n numbers is even or odd public static bool Check(int[] arr, int n) { int x = arr[0]; // assigining the biswise and of given array to x. for (int i = 1; i < n; i++) { x = x & arr[i]; } if (x % 2 == 0) // checking is even or not. return false; // if x is even then returning false. return true; // if x is odd returning true. } // Driver Code public static void Main() { int[] arr = { 2, 12, 20, 36, 38 }; int n = arr.Length; if (Check(arr, n)) Console.WriteLine("Odd"); else Console.WriteLine("Even"); }} |
Output
Even
Time Complexity: O(N)
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 oddvoid 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 codeint 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 approachimport 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 odddef 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 codeif __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 approachusing 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)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



