# 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: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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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.. ` |

*chevron_right*

*filter_none*

**Output:**

Even

**Time Complexity:** O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Check whether bitwise OR of N numbers is Even or Odd
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Bitwise AND of all the odd numbers from 1 to N
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Numbers whose bitwise OR and sum with N are equal
- Queries to check whether bitwise AND of a subarray is even or odd
- Check if bitwise AND of any subset is power of two
- Check if a Number is Odd or Even using Bitwise Operators
- Numbers that are bitwise AND of at least one non-empty sub-array
- Check if a number is divisible by 8 using bitwise operators
- Check if a number is multiple of 9 using bitwise operators
- Check whether bitwise AND of a number with any subset of an array is zero or not
- Check if a number is divisible by 17 using bitwise operators
- Find N distinct numbers whose bitwise Or is equal to K
- Russian Peasant (Multiply two numbers using bitwise operators)
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Check if two numbers are bit rotations of each other or not
- Check whether product of 'n' numbers is even or odd
- Check if one of the numbers is one's complement of the other

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.