Given an array arr[] containing N numbers. The task is to check whether the bitwise-OR of the given N numbers is even or odd.
Examples:
Input : arr[] = { 2, 12, 20, 36, 38 }
Output : Even Bit-wise OR
Input : arr[] = { 3, 9, 12, 13, 15 }
Output : Odd Bit-wise OR
A Simple Solution is to first find the OR of the given N numbers, then check if this OR is even or odd.
Time Complexity: O(N)
A Better Solution is based on bit manipulation and Mathematical facts.
- Bitwise OR of any two even numbers is an even number.
- Bitwise OR of any two odd number is an odd number.
- Bitwise OR of an even and an odd number is an odd number.
Based on the above facts, it can be deduced that if at least one odd number is present in the array then the bitwise OR of whole array will be odd otherwise even.
Below is the implementation of the above approach:
C++
// C++ implementation to check whether // bitwise OR of n numbers is even or odd #include <bits/stdc++.h> using namespace std; // Function to check if bitwise OR // of n numbers is even or odd bool check(int arr[], int n) { for (int i = 0; i < n; i++) { // if at least one odd number is found, // then the bitwise OR of all numbers will be odd if (arr[i] & 1) return true; } // Bitwise OR is an odd number return false; } // Driver Code int main() { int arr[] = { 3, 9, 12, 13, 15 }; int n = sizeof(arr) / sizeof(arr[0]); if (check(arr, n)) cout << "Odd Bit-wise OR"; else cout << "Even Bit-wise OR"; return 0; } |
Java
// Java implementation to check whether // bitwise OR of n numbers is even or odd class GFG { // Function to check if bitwise OR // of n numbers is even or odd static boolean check(int arr[], int n) { for (int i = 0; i < n; i++) { // if at least one odd number is // found, then the bitwise OR of // all numbers will be odd if (arr[i] % 2 == 1) { return true; } } // Bitwise OR is an odd number return false; } // Driver Code public static void main(String args[]) { int arr[] = {3, 9, 12, 13, 15}; int n = arr.length; if (check(arr, n)) { System.out.println("Odd Bit-wise OR"); } else { System.out.println("Even Bit-wise OR"); } } } // This code is contributed // by 29AjayKumar |
Python3
# Python3 implementation to check whether # bitwise OR of n numbers is even or odd # Function to check if bitwise OR # of n numbers is even or odd def check(arr,n): for i in range(n): # if at least one odd number is found, # then the bitwise OR of all numbers # will be odd if arr[i] & 1: return True # Bitwise OR is an odd number return False # Driver code if __name__=='__main__': arr = [3, 9, 12, 13, 15] n = len(arr) if check(arr,n): print("Odd Bit-wise OR") else: print("Even Bit-wise OR") # This code is contributed by # Shrikant13 |
C#
// C# implementation to check whether // bitwise OR of n numbers is even or odd using System; class GFG { // Function to check if bitwise OR // of n numbers is even or odd static bool check(int []arr, int n) { for (int i = 0; i < n; i++) { // if at least one odd number is // found, then the bitwise OR of // all numbers will be odd if (arr[i] % 2 == 1) { return true; } } // Bitwise OR is an odd number return false; } // Driver Code public static void Main() { int []arr = {3, 9, 12, 13, 15}; int n = arr.Length; if (check(arr, n)) { Console.WriteLine("Odd Bit-wise OR"); } else { Console.WriteLine("Even Bit-wise OR"); } } } // This code is contributed // by 29AjayKumar |
PHP
<?php //PHp implementation to check whether // bitwise OR of n numbers is even or odd // Function to check if bitwise OR // of n numbers is even or odd function check($arr, $n) { for ($i = 0; $i < $n; $i++) { // if at least one odd number is found, // then the bitwise OR of all numbers will be odd if ($arr[$i] & 1) return true; } // Bitwise OR is an odd number return false; } // Driver Code $arr = array (3, 9, 12, 13, 15 ); $n = sizeof($arr) / sizeof($arr[0]); if (check($arr, $n)) echo "Odd Bit-wise OR"; else echo "Even Bit-wise OR"; // This code is contributed by ajit ?> |
Odd Bit-wise OR
Time Complexity: O(N) in worst case.
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.
Recommended Posts:
- Check whether bitwise AND of N numbers is Even or Odd
- Queries to check whether bitwise AND of a subarray is even or odd
- Even numbers at even index and odd numbers at odd index
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Check whether product of 'n' numbers is even or odd
- Check if a Number is Odd or Even using Bitwise Operators
- Check whether a given number is even or odd
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Check whether count of odd and even factors of a number are equal
- C program to check whether a given number is even or odd
- Check whether bitwise AND of a number with any subset of an array is zero or not
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Rotate all odd numbers right and all even numbers left in an Array of 1 to N
- Bitwise AND of all the odd numbers from 1 to N
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
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.

