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

📅  最后修改于: 2021-09-03 13:46:52             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是找到给定数组的所有可能的无序对的按位异或。

例子:

天真的方法:这个想法是遍历数组并生成给定数组的所有可能对。最后,打印给定数组的这些对中存在的每个元素的按位异或。请按照以下步骤解决问题:

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

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to get bitwise XOR
// of all possible pairs of
// the given array
int TotalXorPair(int arr[], int N)
{
    // Stores bitwise XOR
    // of all possible pairs
    int totalXOR = 0;
 
    // Generate all possible pairs
    // and calculate bitwise XOR
    // of all possible pairs
    for (int i = 0; i < N; i++) {
        for (int j = i + 1; j < N;
             j++) {
 
            // Calculate bitwise XOR
            // of each pair
            totalXOR ^= arr[i]
                        ^ arr[j];
        }
    }
    return totalXOR;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << TotalXorPair(arr, N);
}


Java
// Java program to implement
// the above approach
class GFG{
   
// Function to get bitwise XOR
// of all possible pairs of
// the given array
public static int TotalXorPair(int arr[],
                               int N)
{
  // Stores bitwise XOR
  // of all possible pairs
  int totalXOR = 0;
 
  // Generate all possible pairs
  // and calculate bitwise XOR
  // of all possible pairs
  for (int i = 0; i < N; i++)
  {
    for (int j = i + 1; j < N; j++)
    {
      // Calculate bitwise XOR
      // of each pair
      totalXOR ^= arr[i] ^ arr[j];
    }
  }
   
  return totalXOR;
}
 
// Driver code   
public static void main(String[] args)
{
  int arr[] = {1, 2, 3, 4};
  int N = arr.length;
  System.out.print(TotalXorPair(arr, N));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to implement
# the above approach
 
# Function to get bitwise XOR
# of all possible pairs of
# the given array
def TotalXorPair(arr, N):
   
    # Stores bitwise XOR
    # of all possible pairs
    totalXOR = 0;
 
    # Generate all possible pairs
    # and calculate bitwise XOR
    # of all possible pairs
    for i in range(0, N):
        for j in range(i + 1, N):
           
            # Calculate bitwise XOR
            # of each pair
            totalXOR ^= arr[i] ^ arr[j];
 
    return totalXOR;
 
# Driver code
if __name__ == '__main__':
   
    arr = [1, 2, 3, 4];
    N = len(arr);
    print(TotalXorPair(arr, N));
 
# This code is contributed by shikhasingrajput


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
   
// Function to get bitwise XOR
// of all possible pairs of
// the given array
public static int TotalXorPair(int []arr,
                               int N)
{
   
  // Stores bitwise XOR
  // of all possible pairs
  int totalXOR = 0;
 
  // Generate all possible pairs
  // and calculate bitwise XOR
  // of all possible pairs
  for(int i = 0; i < N; i++)
  {
    for(int j = i + 1; j < N; j++)
    {
       
      // Calculate bitwise XOR
      // of each pair
      totalXOR ^= arr[i] ^ arr[j];
    }
  }
  return totalXOR;
}
 
// Driver code   
public static void Main(String[] args)
{
  int []arr = {1, 2, 3, 4};
  int N = arr.Length;
   
  Console.Write(TotalXorPair(arr, N));
}
}
 
// This code is contributed by Princi Singh


Javascript


C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to get bitwise XOR
// of all possible pairs of
// the given array
int TotalXorPair(int arr[], int N)
{
    // Stores bitwise XOR
    // of all possible pairs
    int totalXOR = 0;
 
    // Check if N is odd
    if (N % 2 != 0) {
        return 0;
    }
 
    // If N is even then calculate
    // bitwise XOR of all elements
    // of the given array.
    for (int i = 0; i < N; i++) {
        totalXOR ^= arr[i];
    }
    return totalXOR;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << TotalXorPair(arr, N);
}


Java
// Java program to implement
// the above approach
class GFG{
   
// Function to get bitwise XOR
// of all possible pairs of
// the given array
public static int TotalXorPair(int arr[],
                               int N)
{
  // Stores bitwise XOR
  // of all possible pairs
  int totalXOR = 0;
 
  // Check if N is odd
  if( N % 2 != 0 )
  {
    return 0;
  }
 
  // If N is even then calculate
  // bitwise XOR of all elements
  // of the array
  for(int i = 0; i < N; i++)
  {
    totalXOR ^= arr[i];
  }
 
  return totalXOR;
}
 
// Driver code   
public static void main(String[] args)
{
  int arr[] = {1, 2, 3, 4};
  int N = arr.length;
  System.out.print(TotalXorPair(arr, N));
}
}
 
// This code is contributed by math_lover


Python3
# Python3 program to implement
# the above appraoch
 
# Function to get bitwise XOR
# of all possible pairs of
# the given array
def TotalXorPair(arr, N):
 
    # Stores bitwise XOR
    # of all possible pairs
    totalXOR = 0
 
    # Check if N is odd
    if (N % 2 != 0):
        return 0
 
    # If N is even then calculate
    # bitwise XOR of all elements
    # of the given array.
    for i in range(N):
        totalXOR ^= arr[i]
 
    return totalXOR
 
# Driver code
if __name__ == '__main__':
 
    arr = [ 1, 2, 3, 4 ]
    N = len(arr)
     
    print(TotalXorPair(arr, N))
 
# This code is contributed by Shivam Singh


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
   
// Function to get bitwise XOR
// of all possible pairs of
// the given array
static int TotalXorPair(int []arr,
                        int N)
{
     
    // Stores bitwise XOR
    // of all possible pairs
    int totalXOR = 0;
     
    // Check if N is odd
    if (N % 2 != 0)
    {
        return 0;
    }
     
    // If N is even then calculate
    // bitwise XOR of all elements
    // of the array
    for(int i = 0; i < N; i++)
    {
        totalXOR ^= arr[i];
    }
    return totalXOR;
}
 
// Driver code   
public static void Main(String[] args)
{
    int []arr = { 1, 2, 3, 4 };
    int N = arr.Length;
     
    Console.Write(TotalXorPair(arr, N));
}
}
 
// This code is contributed by doreamon_


Javascript


输出
4

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

有效的方法:要优化上述方法,请遵循以下观察:

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

  • 如果N是奇数,则打印0
  • 如果N是偶数,则打印给定数组的所有元素的按位异或的值。

下面是上述方法的实现

C++

// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to get bitwise XOR
// of all possible pairs of
// the given array
int TotalXorPair(int arr[], int N)
{
    // Stores bitwise XOR
    // of all possible pairs
    int totalXOR = 0;
 
    // Check if N is odd
    if (N % 2 != 0) {
        return 0;
    }
 
    // If N is even then calculate
    // bitwise XOR of all elements
    // of the given array.
    for (int i = 0; i < N; i++) {
        totalXOR ^= arr[i];
    }
    return totalXOR;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << TotalXorPair(arr, N);
}

Java

// Java program to implement
// the above approach
class GFG{
   
// Function to get bitwise XOR
// of all possible pairs of
// the given array
public static int TotalXorPair(int arr[],
                               int N)
{
  // Stores bitwise XOR
  // of all possible pairs
  int totalXOR = 0;
 
  // Check if N is odd
  if( N % 2 != 0 )
  {
    return 0;
  }
 
  // If N is even then calculate
  // bitwise XOR of all elements
  // of the array
  for(int i = 0; i < N; i++)
  {
    totalXOR ^= arr[i];
  }
 
  return totalXOR;
}
 
// Driver code   
public static void main(String[] args)
{
  int arr[] = {1, 2, 3, 4};
  int N = arr.length;
  System.out.print(TotalXorPair(arr, N));
}
}
 
// This code is contributed by math_lover

蟒蛇3

# Python3 program to implement
# the above appraoch
 
# Function to get bitwise XOR
# of all possible pairs of
# the given array
def TotalXorPair(arr, N):
 
    # Stores bitwise XOR
    # of all possible pairs
    totalXOR = 0
 
    # Check if N is odd
    if (N % 2 != 0):
        return 0
 
    # If N is even then calculate
    # bitwise XOR of all elements
    # of the given array.
    for i in range(N):
        totalXOR ^= arr[i]
 
    return totalXOR
 
# Driver code
if __name__ == '__main__':
 
    arr = [ 1, 2, 3, 4 ]
    N = len(arr)
     
    print(TotalXorPair(arr, N))
 
# This code is contributed by Shivam Singh

C#

// C# program to implement
// the above approach
using System;
 
class GFG{
   
// Function to get bitwise XOR
// of all possible pairs of
// the given array
static int TotalXorPair(int []arr,
                        int N)
{
     
    // Stores bitwise XOR
    // of all possible pairs
    int totalXOR = 0;
     
    // Check if N is odd
    if (N % 2 != 0)
    {
        return 0;
    }
     
    // If N is even then calculate
    // bitwise XOR of all elements
    // of the array
    for(int i = 0; i < N; i++)
    {
        totalXOR ^= arr[i];
    }
    return totalXOR;
}
 
// Driver code   
public static void Main(String[] args)
{
    int []arr = { 1, 2, 3, 4 };
    int N = arr.Length;
     
    Console.Write(TotalXorPair(arr, N));
}
}
 
// This code is contributed by doreamon_

Javascript


输出
4

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live