Tuesday, December 3, 2024
Google search engine
HomeData Modelling & AIBitwise OR of all unordered pairs from a given array

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




// 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);
}


Java




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


Python3




# 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


C#




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


Javascript




<script>
 
// JavaScript program to implement
// the above approach
 
// Function to find the Bitwise OR of
// all possible pairs from the array
function 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>


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




// 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);
}


Java




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


Python3




# 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


C#




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


Javascript




<script>
 
// JavaScript program to implement
// the above approach
 
// Function to find the bitwise OR of
// all possible pairs of the array
function 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>


Output: 

15

 

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments