📌  相关文章
📜  来自两个给定数组的所有可能的成对和的异或

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

给定两个相等长度的数组A []B [] ,任务是找到给定两个数组的成对和的按位XOR

例子:

天真的方法:解决问题的最简单方法是从两个给定的数组中生成所有可能的对,并计算它们各自的和,并用对的和更新XOR 。最后,打印获得的XOR。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to calculate the sum of
// XOR of the sum of every pair
int XorSum(int A[], int B[], int N)
{
 
    // Stores the XOR of sums
    // of every pair
    int ans = 0;
 
    // Iterate to generate all possible pairs
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
 
            // Update XOR
            ans = ans ^ (A[i] + B[j]);
        }
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
int main()
{
 
    int A[] = { 4, 6, 0, 0, 3, 3 };
 
    int B[] = { 0, 5, 6, 5, 0, 3 };
    int N = sizeof A / sizeof A[0];
 
    cout << XorSum(A, B, N) << endl;
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
 
// Function to calculate the sum of
// XOR of the sum of every pair
static int XorSum(int A[], int B[], int N)
{
     
    // Stores the XOR of sums
    // of every pair
    int ans = 0;
 
    // Iterate to generate all possible pairs
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {
             
            // Update XOR
            ans = ans ^ (A[i] + B[j]);
        }
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
public static void main (String[] args)
{
    int A[] = { 4, 6, 0, 0, 3, 3 };
    int B[] = { 0, 5, 6, 5, 0, 3 };
     
    int N = A.length;
     
    System.out.println(XorSum(A, B, N));
}
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 program to implement
# the above approach
 
# Function to calculate the sum of
# XOR of the sum of every pair
def XorSum(A, B, N):
 
    # Stores the XOR of sums
    # of every pair
    ans = 0
 
    # Iterate to generate all
    # possible pairs
    for i in range(N):
        for j in range(N):
 
            # Update XOR
            ans = ans ^ (A[i] + B[j])
 
    # Return the answer
    return ans
 
# Driver Code
if __name__ == "__main__":
 
    A = [ 4, 6, 0, 0, 3, 3 ]
    B = [ 0, 5, 6, 5, 0, 3 ]
    N = len(A)
 
    print (XorSum(A, B, N))
 
# This code is contributed by chitranayal


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to calculate the sum of
// XOR of the sum of every pair
static int XorSum(int []A, int []B, int N)
{   
    // Stores the XOR of sums
    // of every pair
    int ans = 0;
 
    // Iterate to generate all possible pairs
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < N; j++)
        {           
            // Update XOR
            ans = ans ^ (A[i] + B[j]);
        }
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []A = {4, 6, 0, 0, 3, 3};
    int []B = {0, 5, 6, 5, 0, 3};
    int N = A.Length;   
    Console.WriteLine(XorSum(A, B, N));
}
}
 
// This code is contributed by Rajput-Ji


C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to calculate the
// XOR of the sum of every pair
int XorSum(int A[], int B[], int N)
{
 
    // Stores the maximum bit
    const int maxBit = 29;
 
    int ans = 0;
 
    // Look for all the k-th bit
    for (int k = 0; k < maxBit; k++) {
 
        // Stores the modulo of
        // elements B[] with (2^(k+1))
        int C[N];
 
        for (int i = 0; i < N; i++) {
 
            // Calculate modulo of
            // array B[] with (2^(k+1))
            C[i] = B[i] % (1 << (k + 1));
        }
 
        // Sort the array C[]
        sort(C, C + N);
 
        // Stores the total number
        // whose k-th bit is set
        long long count = 0;
        long long l, r;
 
        for (int i = 0; i < N; i++) {
 
            // Calculate and store the modulo
            // of array A[] with (2^(k+1))
            int x = A[i] % (1 << (k + 1));
 
            // Lower bound to count the number
            // of elements having k-th bit in
            // the range (2^k - x, 2* 2^(k) - x)
            l = lower_bound(C,
                            C + N,
                            (1 << k) - x)
                - C;
 
            r = lower_bound(C,
                            C + N,
                            (1 << k) * 2 - x)
                - C;
 
            // Add total number i.e (r - l)
            // whose k-th bit is one
            count += (r - l);
 
            // Lower bound to count the number
            // of elements having k-th bit in
            // range (3 * 2^k - x, 4*2^(k) - x)
            l = lower_bound(C,
                            C + N,
                            (1 << k) * 3 - x)
                - C;
            r = lower_bound(C,
                            C + N,
                            (1 << k) * 4 - x)
                - C;
 
            count += (r - l);
        }
 
        // If count is even, Xor of
        // k-th bit becomes zero, no
        // need to add to the answer.
        // If count is odd, only then,
        // add to the final answer
        if (count & 1)
            ans += (1 << k);
    }
 
    // Return answer
    return ans;
}
 
// Driver code
int main()
{
    int A[] = { 4, 6, 0, 0, 3, 3 };
    int B[] = { 0, 5, 6, 5, 0, 3 };
    int N = sizeof A / sizeof A[0];
 
    // Function call
    cout << XorSum(A, B, N) << endl;
 
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
     
// Lower bound
static int lower_bound(int[] a, int low,
                       int high, int element)
{
    while(low < high)
    {
        int middle = low + (high - low) / 2;
        if(element > a[middle])
            low = middle + 1;
        else
            high = middle;
    }
    return low;
}
 
// Function to calculate the
// XOR of the sum of every pair
static int XorSum(int A[],
                  int B[], int N)
{
    // Stores the maximum bit
    final int maxBit = 29;
 
    int ans = 0;
 
    // Look for all the k-th bit
    for (int k = 0; k < maxBit; k++)
    {
        // Stores the modulo of
        // elements B[] with (2^(k+1))
        int []C = new int[N];
 
    for (int i = 0; i < N; i++)
    {
        // Calculate modulo of
        // array B[] with (2^(k+1))
        C[i] = B[i] % (1 << (k + 1));
    }
 
    // Sort the array C[]
    Arrays.sort(C);
 
    // Stores the total number
    // whose k-th bit is set
    long count = 0;
    long l, r;
 
    for (int i = 0; i < N; i++)
    {
        // Calculate and store the modulo
        // of array A[] with (2^(k+1))
        int x = A[i] % (1 << (k + 1));
 
        // Lower bound to count
        // the number of elements
        // having k-th bit in
        // the range (2^k - x,
        // 2* 2^(k) - x)
        l = lower_bound(C, 0, N,
                       (1 << k) - x);
 
        r = lower_bound(C, 0, N,
                       (1 << k) *
                        2 - x);
 
        // Add total number i.e
        // (r - l) whose k-th bit is one
        count += (r - l);
 
        // Lower bound to count
        // the number of elements
        // having k-th bit in
        // range (3 * 2^k - x,
        // 4*2^(k) - x)
        l = lower_bound(C, 0, N,
                       (1 << k) *
                        3 - x);
        r = lower_bound(C, 0, N,
                       (1 << k) *
                        4 - x);
 
        count += (r - l);
    }
 
    // If count is even, Xor of
    // k-th bit becomes zero, no
    // need to add to the answer.
    // If count is odd, only then,
    // add to the final answer
    if ((count & 1) != 0)
        ans += (1 << k);
}
 
// Return answer
return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int A[] = {4, 6, 0, 0, 3, 3};
    int B[] = {0, 5, 6, 5, 0, 3};
    int N = A.length;
 
    // Function call
    System.out.print(XorSum(A, B,
                            N) + "\n");
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program to implement
# the above approach
from bisect import bisect, bisect_left, bisect_right
 
# Function to calculate the
# XOR of the sum of every pair
def XorSum(A, B, N):
     
    # Stores the maximum bit
    maxBit = 29
 
    ans = 0
 
    # Look for all the k-th bit
    for k in range(maxBit):
         
        # Stores the modulo of
        # elements B[] with (2^(k+1))
        C = [0] * N
         
        for i in range(N):
             
            # Calculate modulo of
            # array B[] with (2^(k+1))
            C[i] = B[i] % (1 << (k + 1))
 
        # Sort the array C[]
        C = sorted(C)
 
        # Stores the total number
        # whose k-th bit is set
        count = 0
        l, r = 0, 0
 
        for i in range(N):
             
            # Calculate and store the modulo
            # of array A[] with (2^(k+1))
            x = A[i] % (1 << (k + 1))
 
            # Lower bound to count the number
            # of elements having k-th bit in
            # the range (2^k - x, 2* 2^(k) - x)
            l = bisect_left(C, (1 << k) - x)
 
            r = bisect_left(C, (1 << k) * 2 - x)
 
            # Add total number i.e (r - l)
            # whose k-th bit is one
            count += (r - l)
 
            # Lower bound to count the number
            # of elements having k-th bit in
            # range (3 * 2^k - x, 4*2^(k) - x)
            l = bisect_left(C, (1 << k) * 3 - x)
            r = bisect_left(C, (1 << k) * 4 - x)
             
            count += (r - l)
 
        # If count is even, Xor of
        # k-th bit becomes zero, no
        # need to add to the answer.
        # If count is odd, only then,
        # add to the final answer
        if (count & 1):
            ans += (1 << k)
 
    # Return answer
    return ans
 
# Driver code
if __name__ == '__main__':
     
    A = [ 4, 6, 0, 0, 3, 3 ]
    B = [ 0, 5, 6, 5, 0, 3 ]
    N = len(A)
 
    # Function call
    print(XorSum(A, B, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Lower bound
static int lower_bound(int[] a, int low,
                       int high, int element)
{
    while (low < high)
    {
        int middle = low + (high - low) / 2;
        if (element > a[middle])
            low = middle + 1;
        else
            high = middle;
    }
    return low;
}
 
// Function to calculate the
// XOR of the sum of every pair
static int XorSum(int[] A, int[] B, int N)
{
     
    // Stores the maximum bit
    int maxBit = 29;
 
    int ans = 0;
 
    // Look for all the k-th bit
    for(int k = 0; k < maxBit; k++)
    {
         
        // Stores the modulo of
        // elements B[] with (2^(k+1))
        int[] C = new int[N];
 
        for(int i = 0; i < N; i++)
        {
             
            // Calculate modulo of
            // array B[] with (2^(k+1))
            C[i] = B[i] % (1 << (k + 1));
        }
 
        // Sort the array C[]
        Array.Sort(C);
 
        // Stores the total number
        // whose k-th bit is set
        long count = 0;
        long l, r;
 
        for(int i = 0; i < N; i++)
        {
             
            // Calculate and store the modulo
            // of array A[] with (2^(k+1))
            int x = A[i] % (1 << (k + 1));
 
            // Lower bound to count
            // the number of elements
            // having k-th bit in
            // the range (2^k - x,
            // 2* 2^(k) - x)
            l = lower_bound(C, 0, N,
                            (1 << k) - x);
 
            r = lower_bound(C, 0, N,
                            (1 << k) * 2 - x);
 
            // Add total number i.e
            // (r - l) whose k-th bit is one
            count += (r - l);
 
            // Lower bound to count
            // the number of elements
            // having k-th bit in
            // range (3 * 2^k - x,
            // 4*2^(k) - x)
            l = lower_bound(C, 0, N,
                            (1 << k) * 3 - x);
            r = lower_bound(C, 0, N,
                            (1 << k) * 4 - x);
 
            count += (r - l);
        }
 
        // If count is even, Xor of
        // k-th bit becomes zero, no
        // need to add to the answer.
        // If count is odd, only then,
        // add to the final answer
        if ((count & 1) != 0)
            ans += (1 << k);
    }
 
    // Return answer
    return ans;
}
 
// Driver code
public static void Main(string[] args)
{
    int[] A = { 4, 6, 0, 0, 3, 3 };
    int[] B = { 0, 5, 6, 5, 0, 3 };
    int N = A.Length;
 
    // Function call
    Console.Write(XorSum(A, B, N) + "\n");
}
}
 
// This code is contributed by grand_master


输出:
8

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

高效方法:可以使用位操作技术来优化上述方法。请按照以下步骤解决问题:

  • 只考虑K位,任务是计数对(I,J),使得K(A I + B j)的设置的数量。
  • 如果这个数字是奇数,则将X = 2 k加到答案中。我们只对(a i ,b j )以2X为模的值感兴趣。
  • 因此,替换I%(2X)BJb J%(2X)A I,并假设一个IBĴ<2X。
  • 设置(a i + b j)的k位有两种情况:
    • x≤ai + bj <2x
    • 3x≤ai + bj <4x
  • 因此,请按升序对b []进行排序。对于固定的i,满足X≤( ai + b j )<2X的j的集合形成一个间隔。
  • 因此,通过二进制搜索计算此类j的数量。同样,处理第二种情况。

下面是上述方法的实现:

C++

// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to calculate the
// XOR of the sum of every pair
int XorSum(int A[], int B[], int N)
{
 
    // Stores the maximum bit
    const int maxBit = 29;
 
    int ans = 0;
 
    // Look for all the k-th bit
    for (int k = 0; k < maxBit; k++) {
 
        // Stores the modulo of
        // elements B[] with (2^(k+1))
        int C[N];
 
        for (int i = 0; i < N; i++) {
 
            // Calculate modulo of
            // array B[] with (2^(k+1))
            C[i] = B[i] % (1 << (k + 1));
        }
 
        // Sort the array C[]
        sort(C, C + N);
 
        // Stores the total number
        // whose k-th bit is set
        long long count = 0;
        long long l, r;
 
        for (int i = 0; i < N; i++) {
 
            // Calculate and store the modulo
            // of array A[] with (2^(k+1))
            int x = A[i] % (1 << (k + 1));
 
            // Lower bound to count the number
            // of elements having k-th bit in
            // the range (2^k - x, 2* 2^(k) - x)
            l = lower_bound(C,
                            C + N,
                            (1 << k) - x)
                - C;
 
            r = lower_bound(C,
                            C + N,
                            (1 << k) * 2 - x)
                - C;
 
            // Add total number i.e (r - l)
            // whose k-th bit is one
            count += (r - l);
 
            // Lower bound to count the number
            // of elements having k-th bit in
            // range (3 * 2^k - x, 4*2^(k) - x)
            l = lower_bound(C,
                            C + N,
                            (1 << k) * 3 - x)
                - C;
            r = lower_bound(C,
                            C + N,
                            (1 << k) * 4 - x)
                - C;
 
            count += (r - l);
        }
 
        // If count is even, Xor of
        // k-th bit becomes zero, no
        // need to add to the answer.
        // If count is odd, only then,
        // add to the final answer
        if (count & 1)
            ans += (1 << k);
    }
 
    // Return answer
    return ans;
}
 
// Driver code
int main()
{
    int A[] = { 4, 6, 0, 0, 3, 3 };
    int B[] = { 0, 5, 6, 5, 0, 3 };
    int N = sizeof A / sizeof A[0];
 
    // Function call
    cout << XorSum(A, B, N) << endl;
 
    return 0;
}

Java

// Java Program to implement
// the above approach
import java.util.*;
class GFG{
     
// Lower bound
static int lower_bound(int[] a, int low,
                       int high, int element)
{
    while(low < high)
    {
        int middle = low + (high - low) / 2;
        if(element > a[middle])
            low = middle + 1;
        else
            high = middle;
    }
    return low;
}
 
// Function to calculate the
// XOR of the sum of every pair
static int XorSum(int A[],
                  int B[], int N)
{
    // Stores the maximum bit
    final int maxBit = 29;
 
    int ans = 0;
 
    // Look for all the k-th bit
    for (int k = 0; k < maxBit; k++)
    {
        // Stores the modulo of
        // elements B[] with (2^(k+1))
        int []C = new int[N];
 
    for (int i = 0; i < N; i++)
    {
        // Calculate modulo of
        // array B[] with (2^(k+1))
        C[i] = B[i] % (1 << (k + 1));
    }
 
    // Sort the array C[]
    Arrays.sort(C);
 
    // Stores the total number
    // whose k-th bit is set
    long count = 0;
    long l, r;
 
    for (int i = 0; i < N; i++)
    {
        // Calculate and store the modulo
        // of array A[] with (2^(k+1))
        int x = A[i] % (1 << (k + 1));
 
        // Lower bound to count
        // the number of elements
        // having k-th bit in
        // the range (2^k - x,
        // 2* 2^(k) - x)
        l = lower_bound(C, 0, N,
                       (1 << k) - x);
 
        r = lower_bound(C, 0, N,
                       (1 << k) *
                        2 - x);
 
        // Add total number i.e
        // (r - l) whose k-th bit is one
        count += (r - l);
 
        // Lower bound to count
        // the number of elements
        // having k-th bit in
        // range (3 * 2^k - x,
        // 4*2^(k) - x)
        l = lower_bound(C, 0, N,
                       (1 << k) *
                        3 - x);
        r = lower_bound(C, 0, N,
                       (1 << k) *
                        4 - x);
 
        count += (r - l);
    }
 
    // If count is even, Xor of
    // k-th bit becomes zero, no
    // need to add to the answer.
    // If count is odd, only then,
    // add to the final answer
    if ((count & 1) != 0)
        ans += (1 << k);
}
 
// Return answer
return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int A[] = {4, 6, 0, 0, 3, 3};
    int B[] = {0, 5, 6, 5, 0, 3};
    int N = A.length;
 
    // Function call
    System.out.print(XorSum(A, B,
                            N) + "\n");
}
}
 
// This code is contributed by gauravrajput1

Python3

# Python3 program to implement
# the above approach
from bisect import bisect, bisect_left, bisect_right
 
# Function to calculate the
# XOR of the sum of every pair
def XorSum(A, B, N):
     
    # Stores the maximum bit
    maxBit = 29
 
    ans = 0
 
    # Look for all the k-th bit
    for k in range(maxBit):
         
        # Stores the modulo of
        # elements B[] with (2^(k+1))
        C = [0] * N
         
        for i in range(N):
             
            # Calculate modulo of
            # array B[] with (2^(k+1))
            C[i] = B[i] % (1 << (k + 1))
 
        # Sort the array C[]
        C = sorted(C)
 
        # Stores the total number
        # whose k-th bit is set
        count = 0
        l, r = 0, 0
 
        for i in range(N):
             
            # Calculate and store the modulo
            # of array A[] with (2^(k+1))
            x = A[i] % (1 << (k + 1))
 
            # Lower bound to count the number
            # of elements having k-th bit in
            # the range (2^k - x, 2* 2^(k) - x)
            l = bisect_left(C, (1 << k) - x)
 
            r = bisect_left(C, (1 << k) * 2 - x)
 
            # Add total number i.e (r - l)
            # whose k-th bit is one
            count += (r - l)
 
            # Lower bound to count the number
            # of elements having k-th bit in
            # range (3 * 2^k - x, 4*2^(k) - x)
            l = bisect_left(C, (1 << k) * 3 - x)
            r = bisect_left(C, (1 << k) * 4 - x)
             
            count += (r - l)
 
        # If count is even, Xor of
        # k-th bit becomes zero, no
        # need to add to the answer.
        # If count is odd, only then,
        # add to the final answer
        if (count & 1):
            ans += (1 << k)
 
    # Return answer
    return ans
 
# Driver code
if __name__ == '__main__':
     
    A = [ 4, 6, 0, 0, 3, 3 ]
    B = [ 0, 5, 6, 5, 0, 3 ]
    N = len(A)
 
    # Function call
    print(XorSum(A, B, N))
 
# This code is contributed by mohit kumar 29

C#

// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Lower bound
static int lower_bound(int[] a, int low,
                       int high, int element)
{
    while (low < high)
    {
        int middle = low + (high - low) / 2;
        if (element > a[middle])
            low = middle + 1;
        else
            high = middle;
    }
    return low;
}
 
// Function to calculate the
// XOR of the sum of every pair
static int XorSum(int[] A, int[] B, int N)
{
     
    // Stores the maximum bit
    int maxBit = 29;
 
    int ans = 0;
 
    // Look for all the k-th bit
    for(int k = 0; k < maxBit; k++)
    {
         
        // Stores the modulo of
        // elements B[] with (2^(k+1))
        int[] C = new int[N];
 
        for(int i = 0; i < N; i++)
        {
             
            // Calculate modulo of
            // array B[] with (2^(k+1))
            C[i] = B[i] % (1 << (k + 1));
        }
 
        // Sort the array C[]
        Array.Sort(C);
 
        // Stores the total number
        // whose k-th bit is set
        long count = 0;
        long l, r;
 
        for(int i = 0; i < N; i++)
        {
             
            // Calculate and store the modulo
            // of array A[] with (2^(k+1))
            int x = A[i] % (1 << (k + 1));
 
            // Lower bound to count
            // the number of elements
            // having k-th bit in
            // the range (2^k - x,
            // 2* 2^(k) - x)
            l = lower_bound(C, 0, N,
                            (1 << k) - x);
 
            r = lower_bound(C, 0, N,
                            (1 << k) * 2 - x);
 
            // Add total number i.e
            // (r - l) whose k-th bit is one
            count += (r - l);
 
            // Lower bound to count
            // the number of elements
            // having k-th bit in
            // range (3 * 2^k - x,
            // 4*2^(k) - x)
            l = lower_bound(C, 0, N,
                            (1 << k) * 3 - x);
            r = lower_bound(C, 0, N,
                            (1 << k) * 4 - x);
 
            count += (r - l);
        }
 
        // If count is even, Xor of
        // k-th bit becomes zero, no
        // need to add to the answer.
        // If count is odd, only then,
        // add to the final answer
        if ((count & 1) != 0)
            ans += (1 << k);
    }
 
    // Return answer
    return ans;
}
 
// Driver code
public static void Main(string[] args)
{
    int[] A = { 4, 6, 0, 0, 3, 3 };
    int[] B = { 0, 5, 6, 5, 0, 3 };
    int N = A.Length;
 
    // Function call
    Console.Write(XorSum(A, B, N) + "\n");
}
}
 
// This code is contributed by grand_master
输出:
8

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