📌  相关文章
📜  从给定的两个数组中找到所有对的按位与的异或和

📅  最后修改于: 2022-05-13 01:56:07.436000             🧑  作者: Mango

从给定的两个数组中找到所有对的按位与的异或和

给定两个大小分别为NM的数组AB ,任务是计算所有AB对的按位与的异或和

例子:

天真的方法:天真的方法是使用嵌套循环来计算所有对的按位与,然后找到它们的异或和。请按照以下步骤解决问题:

  1. 将变量ans初始化为-1 ,它将存储最终答案。
  2. 遍历数组A ,并执行以下操作:
    1. 对于每个当前元素,遍历数组B ,然后执行以下操作:
      1. 如果ans等于-1 ,则将元素的按位与存储在ans 中。
      2. 否则,将ans的按位 XOR 和元素的按位与存储在ans中。
  3. 返回答案。

下面是上述方法的实现:

C++
// C++ algorithm for the above approach
#include 
using namespace std;
// Function to calculate the XOR sum of all ANDS of all
// pairs on A[] and B[]
int XorSum(int A[], int B[], int N, int M)
{
    // variable to store anshu
    int ans = -1;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            // when there has been no
            // AND of pairs before this
            if (ans == -1)
                ans = (A[i] & B[j]);
            else
                ans ^= (A[i] & B[j]);
        }
    }
    return ans;
}
 
// Driver code
int main()
{
    // Input
    int A[] = { 3, 5 };
    int B[] = { 2, 3 };
    int N = sizeof(A) / sizeof(A[0]);
    int M = sizeof(B) / sizeof(B[0]);
 
    // Function call
    cout << XorSum(A, B, N, M) << endl;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
   
  // Function to calculate the XOR sum of all ANDS of all
// pairs on A[] and B[]
public static int XorSum(int A[], int B[], int N, int M)
{
   
    // variable to store anshu
    int ans = -1;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
           
            // when there has been no
            // AND of pairs before this
            if (ans == -1)
                ans = (A[i] & B[j]);
            else
                ans ^= (A[i] & B[j]);
        }
    }
    return ans;
}
 
// Driver code
    public static void main (String[] args)
    {
       
         // Input
    int A[] = { 3, 5 };
    int B[] = { 2, 3 };
    int N = A.length;
    int M =B.length;
 
    // Function call
    System.out.println(XorSum(A, B, N, M));
       
    }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python3 algorithm for the above approach
 
# Function to calculate the XOR sum of all ANDS of all
# pairs on A and B
def XorSum(A, B, N, M):
   
    # variable to store anshu
    ans = -1
    for i in range(N):
        for j in range(M):
           
            # when there has been no
            # AND of pairs before this
            if (ans == -1):
                ans = (A[i] & B[j])
            else:
                ans ^= (A[i] & B[j])
 
    return ans
   
# Driver code
if __name__ == '__main__':
   
    # Input
    A = [3, 5]
    B = [2, 3]
 
    N = len(A)
    M = len(B)
 
    # Function call
    print (XorSum(A, B, N, M))
 
# This code is contributed by mohit kumar 29.


C#
// C# algorithm for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to calculate the XOR sum of all ANDS of all
// pairs on A[] and B[]
static int XorSum(int []A, int []B, int N, int M)
{
   
    // variable to store anshu
    int ans = -1;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
           
            // when there has been no
            // AND of pairs before this
            if (ans == -1)
                ans = (A[i] & B[j]);
            else
                ans ^= (A[i] & B[j]);
        }
    }
    return ans;
}
 
// Driver code
public static void Main()
{
    // Input]
    int []A = {3, 5};
    int []B = {2, 3};
    int N = A.Length;
    int M = B.Length;
 
    // Function call
    Console.Write(XorSum(A, B, N, M));
}
}
 
// This code is contributed by SURENDER_GANGWAR.


Javascript


C++14
// C++ algorithm for the above approach
#include 
using namespace std;
// Function to calculate the XOR sum of all ANDS of all
// pairs on A[] and B[]
int XorSum(int A[], int B[], int N, int M)
{
    // variable to store xor sums
    // of first array and second
    // array respectively.
    int ans1 = 0, ans2 = 0;
    // Xor sum of first array
    for (int i = 0; i < N; i++)
        ans1 = ans1 ^ A[i];
    // Xor sum of second array
    for (int i = 0; i < M; i++)
        ans2 = ans2 ^ B[i];
    // required answer
    return (ans1 & ans2);
}
// Driver code
int main()
{
    // Input
    int A[] = { 3, 5 };
    int B[] = { 2, 3 };
    int N = sizeof(A) / sizeof(A[0]);
    int M = sizeof(B) / sizeof(B[0]);
 
    // Function call
    cout << XorSum(A, B, N, M) << endl;
}


Java
// Java algorithm for the above approach
import java.io.*;
 
class GFG{
 
// Function to calculate the XOR sum of
// all ANDS of all pairs on A[] and B[]
static int XorSum(int A[], int B[], int N, int M)
{
     
    // Variable to store xor sums
    // of first array and second
    // array respectively.
    int ans1 = 0, ans2 = 0;
     
    // Xor sum of first array
    for(int i = 0; i < N; i++)
        ans1 = ans1 ^ A[i];
         
    // Xor sum of second array
    for(int i = 0; i < M; i++)
        ans2 = ans2 ^ B[i];
         
    // Required answer
    return (ans1 & ans2);
}
 
// Driver code
public static void main(String[] args)
{
     
    // Input
    int A[] = { 3, 5 };
    int B[] = { 2, 3 };
    int N = A.length;
    int M = B.length;
 
    // Function call
    System.out.print(XorSum(A, B, N, M));
}
}
 
// This code is contributed by subham348


Python3
# python 3 algorithm for the above approach
 
# Function to calculate the XOR sum of all ANDS of all
# pairs on A[] and B[]
def XorSum(A, B, N, M):
   
    # variable to store xor sums
    # of first array and second
    # array respectively.
    ans1 = 0
    ans2 = 0
     
    # Xor sum of first array
    for i in range(N):
        ans1 = ans1 ^ A[i]
         
    # Xor sum of second array
    for i in range(M):
        ans2 = ans2 ^ B[i]
         
    # required answer
    return (ans1 & ans2)
 
# Driver code
if __name__ == '__main__':
   
    # Input
    A = [3, 5]
    B = [2, 3]
    N = len(A)
    M = len(B)
 
    # Function call
    print(XorSum(A, B, N, M))
     
    # This code is contributed by bgangwar59.


C#
// C# program for the above approach
 
using System;
 
class GFG {
 
// Function to calculate the XOR sum of
// all ANDS of all pairs on A[] and B[]
static int XorSum(int[] A, int[] B, int N, int M)
{
     
    // Variable to store xor sums
    // of first array and second
    // array respectively.
    int ans1 = 0, ans2 = 0;
     
    // Xor sum of first array
    for(int i = 0; i < N; i++)
        ans1 = ans1 ^ A[i];
         
    // Xor sum of second array
    for(int i = 0; i < M; i++)
        ans2 = ans2 ^ B[i];
         
    // Required answer
    return (ans1 & ans2);
}
 
  // Driver code
  public static void Main (String[] args)
  {
 
    // Input
    int[] A = { 3, 5 };
    int[] B = { 2, 3 };
    int N = A.Length;
    int M = B.Length;
 
    // Function call
    Console.Write(XorSum(A, B, N, M));
  }
}
 
// This code is contributed by code_hunt.


Javascript


输出
0

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

有效的方法:

观察: XOR over AND 的分布性质可以用来解决这个问题。

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

  • 将两个变量ans1ans2初始化为0 ,这将分别存储第一个数组和第二个数组的按位异或和。
  • 遍历A并对每个当前元素:
    • ans1和当前元素的按位异或存储在ans1中。
  • 遍历B并对每个当前元素:
    • ans2和当前元素的按位异或存储在ans2中。
  • 返回ans1&ans2。

下面是上述方法的实现:

C++14

// C++ algorithm for the above approach
#include 
using namespace std;
// Function to calculate the XOR sum of all ANDS of all
// pairs on A[] and B[]
int XorSum(int A[], int B[], int N, int M)
{
    // variable to store xor sums
    // of first array and second
    // array respectively.
    int ans1 = 0, ans2 = 0;
    // Xor sum of first array
    for (int i = 0; i < N; i++)
        ans1 = ans1 ^ A[i];
    // Xor sum of second array
    for (int i = 0; i < M; i++)
        ans2 = ans2 ^ B[i];
    // required answer
    return (ans1 & ans2);
}
// Driver code
int main()
{
    // Input
    int A[] = { 3, 5 };
    int B[] = { 2, 3 };
    int N = sizeof(A) / sizeof(A[0]);
    int M = sizeof(B) / sizeof(B[0]);
 
    // Function call
    cout << XorSum(A, B, N, M) << endl;
}

Java

// Java algorithm for the above approach
import java.io.*;
 
class GFG{
 
// Function to calculate the XOR sum of
// all ANDS of all pairs on A[] and B[]
static int XorSum(int A[], int B[], int N, int M)
{
     
    // Variable to store xor sums
    // of first array and second
    // array respectively.
    int ans1 = 0, ans2 = 0;
     
    // Xor sum of first array
    for(int i = 0; i < N; i++)
        ans1 = ans1 ^ A[i];
         
    // Xor sum of second array
    for(int i = 0; i < M; i++)
        ans2 = ans2 ^ B[i];
         
    // Required answer
    return (ans1 & ans2);
}
 
// Driver code
public static void main(String[] args)
{
     
    // Input
    int A[] = { 3, 5 };
    int B[] = { 2, 3 };
    int N = A.length;
    int M = B.length;
 
    // Function call
    System.out.print(XorSum(A, B, N, M));
}
}
 
// This code is contributed by subham348

Python3

# python 3 algorithm for the above approach
 
# Function to calculate the XOR sum of all ANDS of all
# pairs on A[] and B[]
def XorSum(A, B, N, M):
   
    # variable to store xor sums
    # of first array and second
    # array respectively.
    ans1 = 0
    ans2 = 0
     
    # Xor sum of first array
    for i in range(N):
        ans1 = ans1 ^ A[i]
         
    # Xor sum of second array
    for i in range(M):
        ans2 = ans2 ^ B[i]
         
    # required answer
    return (ans1 & ans2)
 
# Driver code
if __name__ == '__main__':
   
    # Input
    A = [3, 5]
    B = [2, 3]
    N = len(A)
    M = len(B)
 
    # Function call
    print(XorSum(A, B, N, M))
     
    # This code is contributed by bgangwar59.

C#

// C# program for the above approach
 
using System;
 
class GFG {
 
// Function to calculate the XOR sum of
// all ANDS of all pairs on A[] and B[]
static int XorSum(int[] A, int[] B, int N, int M)
{
     
    // Variable to store xor sums
    // of first array and second
    // array respectively.
    int ans1 = 0, ans2 = 0;
     
    // Xor sum of first array
    for(int i = 0; i < N; i++)
        ans1 = ans1 ^ A[i];
         
    // Xor sum of second array
    for(int i = 0; i < M; i++)
        ans2 = ans2 ^ B[i];
         
    // Required answer
    return (ans1 & ans2);
}
 
  // Driver code
  public static void Main (String[] args)
  {
 
    // Input
    int[] A = { 3, 5 };
    int[] B = { 2, 3 };
    int N = A.Length;
    int M = B.Length;
 
    // Function call
    Console.Write(XorSum(A, B, N, M));
  }
}
 
// This code is contributed by code_hunt.

Javascript


输出
0

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