📜  给定数组中所有无序对的按位或

📅  最后修改于: 2021-05-18 01:02:33             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是从给定数组中查找所有可能的无序对的按位XOR。

例子:

方法:解决此问题的最简单方法是遍历数组并生成给定数组的所有可能对。最后,打印给定数组的所有可能对中的每个元素的按位或。请按照以下步骤解决问题:

  • 初始化一个变量,例如totalOR,以存储所有可能对中的每个元素的按位或。
  • 遍历给定数组并从给定数组生成所有可能的对(arr [i],arr [j]),对于每个对(arr [i],arr [j]),更新totalOR =(totalOR | arr )的值[i] | arr [j])
  • 最后,打印totalOR的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
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


C++
// C++ program to implement
// the above approach
 
#include 
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


输出:
15

时间复杂度: O(N 2 )
辅助空间:O(1)

高效方法:为了优化上述方法,该思想基于以下观察结果:

请按照以下步骤解决问题:

  • 初始化一个变量,例如totalOR,以存储阵列中所有可能的无序对的按位或。
  • 遍历数组并更新totalOR =(totalOR | arr [i])的值。
  • 最后,打印totalOR的值。

下面是上述方法的实现

C++

// C++ program to implement
// the above approach
 
#include 
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

Java脚本


输出:
15

时间复杂度: O(N)
辅助空间: O(1)