Check whether bitwise OR of N numbers is Even or Odd
Last Updated :
17 Apr, 2023
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.
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)
{
int x=arr[0];// assigining the biswise or 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
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
import java.util.*;
public class Gfg {
// Function to check if bitwise OR
// of n numbers is even or odd
static boolean check(int[] arr, int n) {
int x = arr[0]; // assigning the bitwise OR of given array to x.
for (int i = 1; i < n; i++) {
x = x | arr[i];
}
// checking if x is even or not.
if (x % 2 == 0)
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 = { 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");
}
}
Python3
# Python 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):
x = arr[0] # assigning the bitwise OR of given array to x.
for i in range(1, n):
x = x | arr[i]
if x % 2 == 0: # checking if x is even or not.
return False # if x is even then returning false.
return True # if x is odd, returning true.
# Driver Code
arr = [3, 9, 12, 13, 15]
n = len(arr)
if check(arr, n):
print("Odd Bit-wise OR")
else:
print("Even Bit-wise OR")
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)
{
int x=arr[0];// assigining the biswise or 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 = { 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");
}
}
}
JavaScript
// JavaScript 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) {
let x = arr[0]; // assigning the bitwise or of given array to x.
for (let i = 1; i < arr.length; i++) {
x = x | arr[i];
}
if (x % 2 == 0) // checking if x is even or not.
return false; // if x is even then returning false.
return true; // if x is odd returning true.
}
// Driver Code
let arr = [3, 9, 12, 13, 15];
if (check(arr))
console.log("Odd Bit-wise OR");
else
console.log("Even Bit-wise OR");
Time Complexity: O(N)
Auxiliary Space: O(1)
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 numbers 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 the 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
?>
JavaScript
<script>
// Javascript 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 (let 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
let arr = [ 3, 9, 12, 13, 15 ];
let n = arr.length;
if (check(arr, n))
document.write("Odd Bit-wise OR");
else
document.write("Even Bit-wise OR");
// This code is contributed by rishavmahato348.
</script>
Time Complexity: O(N) in the worst case.
Auxiliary Space: O(1)
Similar Reads
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,
7 min read
Check whether product of 'n' numbers is even or odd Given an array arr[] of size n, the task is to check whether the product of the given n numbers is even or odd. Even product is even, return true, else false.Examples: Input: arr[] = [2, 4, 3, 5]Output: EvenExplanation: Product = 2 * 4 * 3 * 5 = 120, 120 is even.Input: arr[] = [3, 9, 7, 1]Output: Od
5 min read
Check whether a given number is even or odd Given a number n, check whether it is even or odd. Return true for even and false for odd.Examples: Input: n = 15Output: falseExplanation: 15 % 2 = 1, so 15 is odd .Input: n = 44Output: trueExplanation: 44 % 2 = 0, so 44 is even.Table of Content[Naive Approach] By Finding the Remainder - O(1) Time a
4 min read
Check if a Number is Odd or Even using Bitwise Operators Given a number n, the task is to check whether the number is even or odd using Bitwise Operators.Examples: Input: n = 11 Output: OddInput: n = 10 Output: Even Following Bitwise Operators can be used to check if a number is odd or even:1. Using Bitwise XOR operator: The idea is to check whether the l
6 min read
Check whether XOR of all numbers in a given range is even or odd Given a range [ L, R ], the task is to find if the value of XOR of all natural numbers in the range L to R ( both inclusive ) is even or odd. Print 'Even' if XOR of all numbers in the range is even, otherwise print odd. Examples: Input: L = 1, R= 10 Output: Odd Input: L= 5, R=15 Output: Even A Simpl
8 min read