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++
#include <bits/stdc++.h>
using namespace std;
bool check( int arr[], int n)
{
int x=arr[0];
for ( int i=1;i<n;i++){
x=x | arr[i];
}
if (x%2==0)
return false ;
return true ;
}
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 {
static boolean check( int [] arr, int n) {
int x = arr[ 0 ];
for ( int i = 1 ; i < n; i++) {
x = x | arr[i];
}
if (x % 2 == 0 )
return false ;
return true ;
}
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
def check(arr, n):
x = arr[ 0 ]
for i in range ( 1 , n):
x = x | arr[i]
if x % 2 = = 0 :
return False
return True
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#
using System;
class Gfg{
static bool check( int [] arr, int n)
{
int x=arr[0];
for ( int i=1;i<n;i++){
x=x | arr[i];
}
if (x%2==0)
return false ;
return true ;
}
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
function check(arr) {
let x = arr[0];
for (let i = 1; i < arr.length; i++) {
x = x | arr[i];
}
if (x % 2 == 0)
return false ;
return true ;
}
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++
#include <bits/stdc++.h>
using namespace std;
bool check( int arr[], int n)
{
for ( int i = 0; i < n; i++) {
if (arr[i] & 1)
return true ;
}
return false ;
}
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
class GFG {
static boolean check( int arr[], int n)
{
for ( int i = 0 ; i < n; i++) {
if (arr[i] % 2 == 1 ) {
return true ;
}
}
return false ;
}
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
def check(arr, n):
for i in range (n):
if arr[i] & 1 :
return True
return False
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" )
|
C#
using System;
class GFG {
static bool check( int [] arr, int n)
{
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 1) {
return true ;
}
}
return false ;
}
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" );
}
}
}
|
PHP
<?php
function check( $arr , $n )
{
for ( $i = 0; $i < $n ; $i ++) {
if ( $arr [ $i ] & 1)
return true;
}
return false;
}
$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" ;
?>
|
Javascript
<script>
function check(arr, n)
{
for (let i = 0; i < n; i++)
{
if (arr[i] & 1)
return true ;
}
return false ;
}
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" );
</script>
|
Time Complexity: O(N) in the worst case.
Auxiliary Space: O(1)