Skip to content
Related Articles

Related Articles

Bitwise OR of all unordered pairs from a given array
  • Difficulty Level : Easy
  • Last Updated : 08 Dec, 2020

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:
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 array
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 Code
int main()
{
    int arr[] = { 4, 5, 12, 15 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << TotalBitwiseORPair(arr, N);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.util.*;
class GFG{
   
// Function to find the Bitwise OR of
// all possible pairs from the array
static 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 Code
public 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
 
# Function to find the Bitwise
# OR of all possible pairs
# from the array
def 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 Code
if __name__ == '__main__':
   
    arr = [4, 5, 12, 15]
    N = len(arr)
    print(TotalBitwiseORPair(arr, N))
 
# This code is contributed by Mohit Kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach 
using System;
   
class GFG{
   
// Function to find the Bitwise OR of
// all possible pairs from the array
static 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 Code
public static void Main()
{
    int[] arr = { 4, 5, 12, 15 };
    int N = arr.Length;
     
    Console.WriteLine(TotalBitwiseORPair(arr, N));
}
}
 
// This code is contributed by susmitakundugoaldanga

chevron_right


Output: 

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 array
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 Code
int main()
{
    int arr[] = { 4, 5, 12, 15 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << TotalBitwiseORPair(arr, N);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find the bitwise OR of
// all possible pairs of the array
static 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 Code
public 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to implement
# the above approach
 
# Function to find the bitwise OR of
# all possible pairs of the array
def 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 Code
if __name__ == '__main__':
    arr = [4, 5, 12, 15];
    N = len(arr);
 
    print(TotalBitwiseORPair(arr, N));
 
    # This code is contributed by shikhasingrajput

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to find the bitwise OR of
// all possible pairs of the array
static 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 Code
public 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

chevron_right


Output: 

15

 

Time Complexity: O(N)
Auxiliary Space: O(1)

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :