Bitwise OR of all unordered pairs from a given array - GeeksforGeeks

23 Apr, 2021
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++
Java
Python3
C#
Javascript
// C++ program to implement// the above approach #include <bits/stdc++.h>using namespace std; // Function to find the Bitwise OR of// all possible pairs from the arrayint TotalBitwiseORPair(int arr[], int N){     // Stores bitwise OR of all    // possible pairs from arr[]    int totalOR = 0;     // Traverse the array and calculate    // bitwise OR of all possible pairs    for (int i = 0; i < N; i++) {        for (int j = i + 1; j < N;             j++) {             // Update totalOR            totalOR |= (arr[i] | arr[j]);        }    }     // Return Bitwise OR of all    // possible pairs from arr[]    return totalOR;} // Driver Codeint main(){    int arr[] = { 4, 5, 12, 15 };    int N = sizeof(arr) / sizeof(arr[0]);    cout << TotalBitwiseORPair(arr, N);}
// Java program to implement// the above approachimport java.util.*;class GFG{   // Function to find the Bitwise OR of// all possible pairs from the arraystatic int TotalBitwiseORPair(int arr[],                              int N){  // Stores bitwise OR of all  // possible pairs from arr[]  int totalOR = 0;   // Traverse the array and  // calculate bitwise OR of  // all possible pairs  for (int i = 0; i < N; i++)  {    for (int j = i + 1; j < N;         j++)    {      // Update totalOR      totalOR |= (arr[i] |                  arr[j]);    }  }   // Return Bitwise OR of all  // possible pairs from arr[]  return totalOR;} // Driver Codepublic static void main(String[] args){  int arr[] = {4, 5, 12, 15};  int N = arr.length;  System.out.print(TotalBitwiseORPair(arr, N));}} // This code is contributed by sanjoy_62
# Python3 program to implement# the above approach # Function to find the Bitwise# OR of all possible pairs# from the arraydef TotalBitwiseORPair(arr, N):     # Stores bitwise OR of all    # possible pairs from arr[]    totalOR = 0     # Traverse the array and    # calculate bitwise OR of    # all possible pairs    for i in range(N):        for j in range(i + 1, N):             # Update totalOR            totalOR |= (arr[i] | arr[j])     # Return Bitwise OR of all    # possible pairs from arr[]    return totalOR # Driver Codeif __name__ == '__main__':       arr = [4, 5, 12, 15]    N = len(arr)    print(TotalBitwiseORPair(arr, N)) # This code is contributed by Mohit Kumar 29
// C# program to implement// the above approach using System;   class GFG{   // Function to find the Bitwise OR of// all possible pairs from the arraystatic int TotalBitwiseORPair(int[] arr,                              int N){       // Stores bitwise OR of all  // possible pairs from arr[]  int totalOR = 0;    // Traverse the array and  // calculate bitwise OR of  // all possible pairs  for(int i = 0; i < N; i++)  {    for(int j = i + 1; j < N; j++)    {               // Update totalOR      totalOR |= (arr[i] | arr[j]);    }  }    // Return Bitwise OR of all  // possible pairs from arr[]  return totalOR;}  // Driver Codepublic static void Main(){    int[] arr = { 4, 5, 12, 15 };    int N = arr.Length;         Console.WriteLine(TotalBitwiseORPair(arr, N));}} // This code is contributed by susmitakundugoaldanga
<script> // JavaScript program to implement// the above approach // Function to find the Bitwise OR of// all possible pairs from the arrayfunction TotalBitwiseORPair(arr, N){     // Stores bitwise OR of all    // possible pairs from arr[]    let totalOR = 0;     // Traverse the array and calculate    // bitwise OR of all possible pairs    for (let i = 0; i < N; i++) {        for (let j = i + 1; j < N;            j++) {             // Update totalOR            totalOR |= (arr[i] | arr[j]);        }    }     // Return Bitwise OR of all    // possible pairs from arr[]    return totalOR;} // Driver Code     let arr = [ 4, 5, 12, 15 ];    let N = arr.length;    document.write(TotalBitwiseORPair(arr, N)); // This code is contributed by Surbhi Tyagi </script>
15
 
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++
Java
Python3
C#
Javascript
// C++ program to implement// the above approach #include <bits/stdc++.h>using namespace std; // Function to find the bitwise OR of// all possible pairs of the arrayint TotalBitwiseORPair(int arr[], int N){     // Stores bitwise OR of all    // possible pairs of arr[]    int totalOR = 0;     // Traverse the array arr[]    for (int i = 0; i < N; i++) {         // Update totalOR        totalOR |= arr[i];    }     // Return bitwise OR of all    // possible pairs of arr[]    return totalOR;} // Driver Codeint main(){    int arr[] = { 4, 5, 12, 15 };    int N = sizeof(arr) / sizeof(arr[0]);    cout << TotalBitwiseORPair(arr, N);}
// Java program to implement// the above approachimport java.util.*; class GFG{ // Function to find the bitwise OR of// all possible pairs of the arraystatic int TotalBitwiseORPair(int arr[],                              int N){         // Stores bitwise OR of all    // possible pairs of arr[]    int totalOR = 0;     // Traverse the array arr[]    for(int i = 0; i < N; i++)    {                 // Update totalOR        totalOR |= arr[i];    }     // Return bitwise OR of all    // possible pairs of arr[]    return totalOR;} // Driver Codepublic static void main(String[] args){    int arr[] = { 4, 5, 12, 15 };    int N = arr.length;         System.out.print(TotalBitwiseORPair(arr, N));}} // This code is contributed by gauravrajput1
# Python program to implement# the above approach # Function to find the bitwise OR of# all possible pairs of the arraydef TotalBitwiseORPair(arr, N):       # Stores bitwise OR of all    # possible pairs of arr    totalOR = 0;     # Traverse the array arr    for i in range(N):               # Update totalOR        totalOR |= arr[i];     # Return bitwise OR of all    # possible pairs of arr    return totalOR; # Driver Codeif __name__ == '__main__':    arr = [4, 5, 12, 15];    N = len(arr);     print(TotalBitwiseORPair(arr, N));     # This code is contributed by shikhasingrajput
// C# program to implement// the above approachusing System; class GFG{ // Function to find the bitwise OR of// all possible pairs of the arraystatic int TotalBitwiseORPair(int []arr,                              int N){         // Stores bitwise OR of all    // possible pairs of []arr    int totalOR = 0;     // Traverse the array []arr    for(int i = 0; i < N; i++)    {                 // Update totalOR        totalOR |= arr[i];    }     // Return bitwise OR of all    // possible pairs of []arr    return totalOR;} // Driver Codepublic static void Main(String[] args){    int []arr = { 4, 5, 12, 15 };    int N = arr.Length;         Console.Write(TotalBitwiseORPair(arr, N));}} // This code is contributed by Princi Singh
<script> // JavaScript program to implement// the above approach // Function to find the bitwise OR of// all possible pairs of the arrayfunction TotalBitwiseORPair(arr, N){          // Stores bitwise OR of all    // possible pairs of arr[]    let totalOR = 0;      // Traverse the array arr[]    for(let i = 0; i < N; i++)    {                  // Update totalOR        totalOR |= arr[i];    }      // Return bitwise OR of all    // possible pairs of arr[]    return totalOR;}   // Driver Code     let arr = [ 4, 5, 12, 15 ];    let N = arr.length;          document.write(TotalBitwiseORPair(arr, N)); </script>
15
 
Time Complexity: O(N)Auxiliary Space: O(1)
mohit kumar 29
sanjoy_62
susmitakundugoaldanga
GauravRajput1
princi singh
shikhasingrajput
surbhityagi15
splevel62
Bitwise-OR
Arrays
Bit Magic
Mathematical
Arrays
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Find duplicates in O(n) time and O(1) extra space | Set 1
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
How to swap two numbers without using a temporary variable?