给定分别由N和M个整数组成的两个数组arr1 []和arr2 [] ,任务是通过从arr1 []和arr2 []中选择一个元素来打印所有对的按位与的按位XOR。
例子:
Input: arr1[] = {1, 2, 3}, arr2[] = {6, 5}
Output: 0
Explanation:
Bitwise AND of the pair (arr1[0], arr2[]) = 1 & 6 = 0.
Bitwise AND of the pair (arr1[0], arr2[1]) = 1 & 5 = 1.
Bitwise AND of the pair (arr1[1], arr2[0]) = 2 & 6 = 2.
Bitwise AND of the pair (arr1[1], arr2[1]) = 2 & 5 = 0.
Bitwise AND of the pair (arr1[2], arr2[0]) = 3 & 6 = 2.
Bitwise AND of the pair (arr1[2], arr2[1]) = 3 & 5 = 1.
Therefore, Bitwise XOR of the obtained Bitwise AND values = 0 ^ 1 ^ 2 ^ 0^ 2 ^ 1 = 0.
Input: arr1[] = {12}, arr2[] = {4}
Output: 4
天真的方法:最简单的方法是通过从arr1 []中选择一个元素并从arr2 []中选择另一个元素,然后计算结果对的所有按位与的按位XOR,找到所有可能对中的按位与。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the Bitwise XOR
// of Bitwise AND of all pairs from
// the arrays arr1[] and arr2[]
int findXORS(int arr1[], int arr2[],
int N, int M)
{
// Stores the result
int res = 0;
// Iterate over the range [0, N - 1]
for (int i = 0; i < N; i++) {
// Iterate over the range [0, M - 1]
for (int j = 0; j < M; j++) {
// Stores Bitwise AND of
// the pair {arr1[i], arr2[j]}
int temp = arr1[i] & arr2[j];
// Update res
res ^= temp;
}
}
// Return the res
return res;
}
// Driver Code
int main()
{
// Input
int arr1[] = { 1, 2, 3 };
int arr2[] = { 6, 5 };
int N = sizeof(arr1) / sizeof(arr1[0]);
int M = sizeof(arr2) / sizeof(arr2[0]);
cout << findXORS(arr1, arr2, N, M);
return 0;
}
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the Bitwise XOR
// of Bitwise AND of all pairs from
// the arrays arr1[] and arr2[]
int findXORS(int arr1[], int arr2[],
int N, int M)
{
// Stores XOR of array arr1[]
int XORS1 = 0;
// Stores XOR of array arr2[]
int XORS2 = 0;
// Traverse the array arr1[]
for (int i = 0; i < N; i++) {
XORS1 ^= arr1[i];
}
// Traverse the array arr2[]
for (int i = 0; i < M; i++) {
XORS2 ^= arr2[i];
}
// Return the result
return XORS1 and XORS2;
}
// Driver Code
int main()
{
// Input
int arr1[] = { 1, 2, 3 };
int arr2[] = { 6, 5 };
int N = sizeof(arr1) / sizeof(arr1[0]);
int M = sizeof(arr2) / sizeof(arr2[0]);
cout << findXORS(arr1, arr2, N, M);
return 0;
}
0
时间复杂度: O(N * M)
辅助空间: O(1)
高效的方法:可以基于以下观察来优化上述方法:
- 按位Xor和按位与运算具有加法和分布式属性。
- 因此,将数组视为arr1 [] = {A,B}和arr2 [] = {X,Y}:
- (A AND X)XOR(A AND Y)XOR(B AND X)XOR(B AND Y)
- (A AND(X XOR Y))XOR(B AND(X XOR Y))
- (A XOR B)和(X XOR Y)
- 因此,从上述步骤开始,任务减少为找到arr1 []和arr2 []的按位XOR的按位与。
请按照以下步骤解决问题:
- 找到数组arr1 []的每个数组元素的按位Xor并将其存储在变量XORS1中。
- 找到数组arr2 []的每个数组元素的按位Xor并将其存储在变量XORS2中。
- 最后,将结果打印为XORS1和XORS2的按位与。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the Bitwise XOR
// of Bitwise AND of all pairs from
// the arrays arr1[] and arr2[]
int findXORS(int arr1[], int arr2[],
int N, int M)
{
// Stores XOR of array arr1[]
int XORS1 = 0;
// Stores XOR of array arr2[]
int XORS2 = 0;
// Traverse the array arr1[]
for (int i = 0; i < N; i++) {
XORS1 ^= arr1[i];
}
// Traverse the array arr2[]
for (int i = 0; i < M; i++) {
XORS2 ^= arr2[i];
}
// Return the result
return XORS1 and XORS2;
}
// Driver Code
int main()
{
// Input
int arr1[] = { 1, 2, 3 };
int arr2[] = { 6, 5 };
int N = sizeof(arr1) / sizeof(arr1[0]);
int M = sizeof(arr2) / sizeof(arr2[0]);
cout << findXORS(arr1, arr2, N, M);
return 0;
}
0
时间复杂度: O(N + M)
辅助空间: O(1)