📌  相关文章
📜  每个数组元素与所有其他数组元素的按位异或之和

📅  最后修改于: 2021-09-04 08:19:57             🧑  作者: Mango

给定一个长度为N的数组arr[] ,每个数组元素的任务是打印其与所有其他数组元素的按位异或之和。

例子:

朴素的方法:这个想法是遍历数组,对于每个数组元素,遍历数组并计算其与所有其他数组元素的按位异或之和。

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

有效的方法:为了优化上述方法,想法是使用按位异或的特性,即异或上的相似位给出0 ,否则给出1 。请按照以下步骤解决问题:

  • 计算位置i处设置位的频率,其中0 <= i <= 32 ,跨越频率数组中数组的所有元素。
  • 对于数组的每个元素X ,通过运行从i=0 到 32的循环来计算异或和,并检查是否设置了X 的i位。
    • 如果是,则将(N – frequency[i])*2 i 添加到异或和,因为在此位置X的设置位将使所有设置位为零,所有未设置位为1
    • 否则,将frequency[i] * 2 i 添加到异或和。
  • 计算数组中每个元素的所有异或和的总和并作为答案返回。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate for each array
// element, sum of its Bitwise XOR with
// all other array elements
void XOR_for_every_i(int A[], int N)
{
    // Declare an array of size 64
    // to store count of each bit
    int frequency_of_bits[32]{};
 
    // Traversing the array
    for (int i = 0; i < N; i++) {
 
        int bit_position = 0;
        int M = A[i];
 
        while (M) {
 
            // Check if bit is present of not
            if (M & 1) {
                frequency_of_bits[bit_position] += 1;
            }
 
            // Increase the bit position
            bit_position += 1;
 
            // Reduce the number to half
            M >>= 1;
        }
    }
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
        int M = A[i];
 
        // Stores the bit position
        int value_at_that_bit = 1;
 
        // Stores the sum of Bitwise XOR
        int XOR_sum = 0;
 
        for (int bit_position = 0;
             bit_position < 32;
             bit_position++) {
 
            // Check if bit is present of not
            if (M & 1) {
                XOR_sum
                    += (N
                        - frequency_of_bits[bit_position])
                       * value_at_that_bit;
            }
            else {
                XOR_sum
                    += (frequency_of_bits[bit_position])
                       * value_at_that_bit;
            }
 
            // Reduce the number to its half
            M >>= 1;
 
            value_at_that_bit <<= 1;
        }
 
        // Print the sum for A[i]
        cout << XOR_sum << ' ';
    }
 
    return;
}
 
// Driver Code
int main()
{
 
    // Given array
    int A[] = { 1, 2, 3 };
 
    // Given N
    int N = sizeof(A) / sizeof(A[0]);
 
    // Function Call
    XOR_for_every_i(A, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
      
// Function to calculate for each array
// element, sum of its Bitwise XOR with
// all other array elements
static void XOR_for_every_i(int A[], int N)
{
     
    // Declare an array of size 64
    // to store count of each bit
    int frequency_of_bits[] = new int[32];
  
    // Traversing the array
    for(int i = 0; i < N; i++)
    {
        int bit_position = 0;
        int M = A[i];
  
        while (M != 0)
        {
             
            // Check if bit is present of not
            if ((M & 1) != 0)
            {
                frequency_of_bits[bit_position] += 1;
            }
  
            // Increase the bit position
            bit_position += 1;
  
            // Reduce the number to half
            M >>= 1;
        }
    }
  
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
        int M = A[i];
  
        // Stores the bit position
        int value_at_that_bit = 1;
  
        // Stores the sum of Bitwise XOR
        int XOR_sum = 0;
  
        for(int bit_position = 0;
                bit_position < 32;
                bit_position++)
        {
             
            // Check if bit is present of not
            if ((M & 1) != 0)
            {
                XOR_sum += (N -
                            frequency_of_bits[bit_position]) *
                            value_at_that_bit;
            }
            else
            {
                XOR_sum += (frequency_of_bits[bit_position]) *
                            value_at_that_bit;
            }
  
            // Reduce the number to its half
            M >>= 1;
  
            value_at_that_bit <<= 1;
        }
  
        // Print the sum for A[i]
        System.out.print( XOR_sum + " ");
    }
    return;
}
  
// Driver code
public static void main(String[] args)
{
     
    // Given array
    int A[] = { 1, 2, 3 };
  
    // Given N
    int N = A.length;
  
    // Function Call
    XOR_for_every_i(A, N);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program for the above approach
  
# Function to calculate for each array
# element, sum of its Bitwise XOR with
# all other array elements
def XOR_for_every_i(A, N):
     
    # Declare an array of size 64
    # to store count of each bit
    frequency_of_bits = [0] * 32
  
    # Traversing the array
    for i in range(N):
        bit_position = 0
        M = A[i]
  
        while (M):
             
            # Check if bit is present of not
            if (M & 1 != 0):
                frequency_of_bits[bit_position] += 1
  
            # Increase the bit position
            bit_position += 1
  
            # Reduce the number to half
            M >>= 1
     
    # Traverse the array
    for i in range(N):
        M = A[i]
  
        # Stores the bit position
        value_at_that_bit = 1
  
        # Stores the sum of Bitwise XOR
        XOR_sum = 0
  
        for bit_position in range(32):
  
            # Check if bit is present of not
            if (M & 1 != 0):
                XOR_sum += ((N - frequency_of_bits[bit_position]) *
                            value_at_that_bit)
             
            else:
                XOR_sum += ((frequency_of_bits[bit_position]) *
                            value_at_that_bit)
             
            # Reduce the number to its half
            M >>= 1
  
            value_at_that_bit <<= 1
  
        # Print the sum for A[i]
        print(XOR_sum, end = " ")
     
    return
 
# Driver Code
 
# Given arr1[]
A = [ 1, 2, 3 ]
 
# Size of N
N = len(A)
  
# Function Call
XOR_for_every_i(A, N)
 
# This code is contributed by code_hunt


C#
// C# program for the above approach
using System;
class GFG
{
      
// Function to calculate for each array
// element, sum of its Bitwise XOR with
// all other array elements
static void XOR_for_every_i(int[] A, int N)
{
      
    // Declare an array of size 64
    // to store count of each bit
    int[] frequency_of_bits = new int[32];
   
    // Traversing the array
    for(int i = 0; i < N; i++)
    {
        int bit_position = 0;
        int M = A[i];
   
        while (M != 0)
        {
              
            // Check if bit is present of not
            if ((M & 1) != 0)
            {
                frequency_of_bits[bit_position] += 1;
            }
   
            // Increase the bit position
            bit_position += 1;
   
            // Reduce the number to half
            M >>= 1;
        }
    }
   
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
        int M = A[i];
   
        // Stores the bit position
        int value_at_that_bit = 1;
   
        // Stores the sum of Bitwise XOR
        int XOR_sum = 0;
   
        for(int bit_position = 0;
                bit_position < 32;
                bit_position++)
        {
              
            // Check if bit is present of not
            if ((M & 1) != 0)
            {
                XOR_sum += (N -
                            frequency_of_bits[bit_position]) *
                            value_at_that_bit;
            }
            else
            {
                XOR_sum += (frequency_of_bits[bit_position]) *
                            value_at_that_bit;
            }
   
            // Reduce the number to its half
            M >>= 1; 
            value_at_that_bit <<= 1;
        }
   
        // Print the sum for A[i]
        Console.Write( XOR_sum + " ");
    }
    return;
}
  
// Driver Code
public static void Main()
{
   
    // Given array
    int[] A = { 1, 2, 3 };
   
    // Given N
    int N = A.Length;
   
    // Function Call
    XOR_for_every_i(A, N);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
5 4 3

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

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