Bitwise OR of all unordered pairs from a given array
Given an array arr[] of size N, the task is to find the Bitwise XOR of all possible unordered pairs from the given array.
Examples:
Input: arr[] = {1, 5, 3, 7}
Output: 7
Explanation:
All possible unordered pairs are (1, 5), (1, 3), (1, 7), (5, 3), (5, 7), (3, 7)
Bitwise OR of all possible pairs are = { ( 1 | 5 ) | ( 1 | 3 ) | ( 1 | 7 ) | ( 5 | 3 ) | ( 5 | 7 ) | ( 3 | 7 ) }
Therefore, the required output is 7.
Input: arr[] = {4, 5, 12, 15}
Output: 15
Approach: The simplest approach to solve this problem is to traverse the array and generate all possible pairs of the given array. Finally, print the Bitwise OR of each element of all possible pairs of the given array. Follow the steps below to solve the problem:
- Initialize a variable, say totalOR, to store Bit-wise OR of each element of all possible pairs.
- Traverse the given array and generate all possible pairs(arr[i], arr[j]) from the given array and for each pair (arr[i], arr[j]), update the value of totalOR = (totalOR | arr[i] | arr[j]).
- Finally, print the value of totalOR.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int TotalBitwiseORPair( int arr[], int N)
{
int totalOR = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N;
j++) {
totalOR |= (arr[i] | arr[j]);
}
}
return totalOR;
}
int main()
{
int arr[] = { 4, 5, 12, 15 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << TotalBitwiseORPair(arr, N);
}
|
Java
import java.util.*;
class GFG{
static int TotalBitwiseORPair( int arr[],
int N)
{
int totalOR = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = i + 1 ; j < N;
j++)
{
totalOR |= (arr[i] |
arr[j]);
}
}
return totalOR;
}
public static void main(String[] args)
{
int arr[] = { 4 , 5 , 12 , 15 };
int N = arr.length;
System.out.print(TotalBitwiseORPair(arr, N));
}
}
|
Python3
def TotalBitwiseORPair(arr, N):
totalOR = 0
for i in range (N):
for j in range (i + 1 , N):
totalOR | = (arr[i] | arr[j])
return totalOR
if __name__ = = '__main__' :
arr = [ 4 , 5 , 12 , 15 ]
N = len (arr)
print (TotalBitwiseORPair(arr, N))
|
C#
using System;
class GFG{
static int TotalBitwiseORPair( int [] arr,
int N)
{
int totalOR = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = i + 1; j < N; j++)
{
totalOR |= (arr[i] | arr[j]);
}
}
return totalOR;
}
public static void Main()
{
int [] arr = { 4, 5, 12, 15 };
int N = arr.Length;
Console.WriteLine(TotalBitwiseORPair(arr, N));
}
}
|
Javascript
<script>
function TotalBitwiseORPair(arr, N)
{
let totalOR = 0;
for (let i = 0; i < N; i++) {
for (let j = i + 1; j < N;
j++) {
totalOR |= (arr[i] | arr[j]);
}
}
return totalOR;
}
let arr = [ 4, 5, 12, 15 ];
let N = arr.length;
document.write(TotalBitwiseORPair(arr, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach the idea is based on the following observations:
1 | 1 | 1 | …..(n times) = 1
0 | 0 | 0 | …..(n times) = 0
Therefore, (a | a | a | …. (n times)) = a
Follow the steps below to solve the problem:
- Initialize a variable, say totalOR to store the bitwise OR of all possible unordered pairs of the array.
- Traverse the array and update the value of totalOR = (totalOR | arr[i]).
- Finally, print the value of totalOR.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
int TotalBitwiseORPair( int arr[], int N)
{
int totalOR = 0;
for ( int i = 0; i < N; i++) {
totalOR |= arr[i];
}
return totalOR;
}
int main()
{
int arr[] = { 4, 5, 12, 15 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << TotalBitwiseORPair(arr, N);
}
|
Java
import java.util.*;
class GFG{
static int TotalBitwiseORPair( int arr[],
int N)
{
int totalOR = 0 ;
for ( int i = 0 ; i < N; i++)
{
totalOR |= arr[i];
}
return totalOR;
}
public static void main(String[] args)
{
int arr[] = { 4 , 5 , 12 , 15 };
int N = arr.length;
System.out.print(TotalBitwiseORPair(arr, N));
}
}
|
Python3
def TotalBitwiseORPair(arr, N):
totalOR = 0 ;
for i in range (N):
totalOR | = arr[i];
return totalOR;
if __name__ = = '__main__' :
arr = [ 4 , 5 , 12 , 15 ];
N = len (arr);
print (TotalBitwiseORPair(arr, N));
|
C#
using System;
class GFG{
static int TotalBitwiseORPair( int []arr,
int N)
{
int totalOR = 0;
for ( int i = 0; i < N; i++)
{
totalOR |= arr[i];
}
return totalOR;
}
public static void Main(String[] args)
{
int []arr = { 4, 5, 12, 15 };
int N = arr.Length;
Console.Write(TotalBitwiseORPair(arr, N));
}
}
|
Javascript
<script>
function TotalBitwiseORPair(arr, N)
{
let totalOR = 0;
for (let i = 0; i < N; i++)
{
totalOR |= arr[i];
}
return totalOR;
}
let arr = [ 4, 5, 12, 15 ];
let N = arr.length;
document.write(TotalBitwiseORPair(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
23 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...