📌  相关文章
📜  两个数组 XOR 之间偶数设置位的计数

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

给定两个数组A[] 和 B[] 分别具有NM 个正元素。任务是为数组 B 的每个元素计算数组 A 中具有偶数位异或中设置位的元素的数量。
例子:

朴素的方法:这个想法是计算数组B[]中每个元素与数组A[] 中每个元素的异或,并计算具有偶数设置位的数字。
时间复杂度: O(N*M),其中 N 和 M 分别是数组A[]B[]的长度。
高效的方法:这个想法是使用 XOR 的属性。对于任何两个数字,如果两个数字的设置位计数是偶数或奇数,则两个数字异或后的设置位计数是偶数
以下是基于上述属性的步骤:

  1. 计算数组A[] 中具有偶数(比如a )和奇数(比如b )设置位的元素的数量。
  2. 对于数组B[]中的每个元素:
    • 如果当前元素有偶数设置位,则数组A[] 中与当前元素异或设置偶数为偶数的元素为a
    • 如果当前元素的设置位为奇数,则数组A[] 中与当前元素异或设置位为偶数的元素为b

下面是上述方法的实现:

CPP
// C++ program for the above approach
#include 
using namespace std;
 
// Function that count the XOR of B[]
// with all the element in A[] having
// even set bit
void countEvenBit(int A[], int B[], int n, int m)
{
    int i, j, cntOdd = 0, cntEven = 0;
    for (i = 0; i < n; i++) {
 
        // Count the set bits in A[i]
        int x = __builtin_popcount(A[i]);
 
        // check for even or Odd
        if (x & 1) {
            cntEven++;
        }
        else {
            cntOdd++;
        }
    }
 
    // To store the count of element for
    // B[] such that XOR with all the
    // element in A[] having even set bit
    int CountB[m];
 
    for (i = 0; i < m; i++) {
 
        // Count set bit for B[i]
        int x = __builtin_popcount(B[i]);
 
        // check for Even or Odd
        if (x & 1) {
            CountB[i] = cntEven;
        }
        else {
            CountB[i] = cntOdd;
        }
    }
 
    for (i = 0; i < m; i++) {
        cout << CountB[i] << ' ';
    }
}
 
// Driver Code
int main()
{
    int A[] = { 4, 2, 15, 9, 8, 8 };
    int B[] = { 3, 4, 22 };
 
    countEvenBit(A, B, 6, 3);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
  
// Function that count the XOR of B[]
// with all the element in A[] having
// even set bit
static void countEvenBit(int A[], int B[], int n, int m)
{
    int i, j, cntOdd = 0, cntEven = 0;
    for (i = 0; i < n; i++) {
  
        // Count the set bits in A[i]
        int x = Integer.bitCount(A[i]);
  
        // check for even or Odd
        if (x % 2 == 1) {
            cntEven++;
        }
        else {
            cntOdd++;
        }
    }
  
    // To store the count of element for
    // B[] such that XOR with all the
    // element in A[] having even set bit
    int []CountB = new int[m];
  
    for (i = 0; i < m; i++) {
  
        // Count set bit for B[i]
        int x = Integer.bitCount(B[i]);
  
        // check for Even or Odd
        if (x%2 == 1) {
            CountB[i] = cntEven;
        }
        else {
            CountB[i] = cntOdd;
        }
    }
  
    for (i = 0; i < m; i++) {
        System.out.print(CountB[i] +" ");
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int A[] = { 4, 2, 15, 9, 8, 8 };
    int B[] = { 3, 4, 22 };
  
    countEvenBit(A, B, 6, 3);
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 program for the above approach
 
# Function that count the XOR of B
# with all the element in A having
# even set bit
def countEvenBit(A, B, n, m):
 
    i, j, cntOdd = 0, 0, 0
    cntEven = 0
    for i in range(n):
 
        # Count the set bits in A[i]
        x = bin(A[i])[2:].count('1')
 
        # check for even or Odd
        if (x & 1):
            cntEven += 1
 
        else :
            cntOdd += 1
 
    # To store the count of element for
    # B such that XOR with all the
    # element in A having even set bit
    CountB = [0]*m
 
    for i in range(m):
 
        # Count set bit for B[i]
        x = bin(B[i])[2:].count('1')
 
        # check for Even or Odd
        if (x & 1):
            CountB[i] = cntEven
 
        else:
            CountB[i] = cntOdd
 
    for i in range(m):
        print(CountB[i], end=" ")
 
# Driver Code
if __name__ == '__main__':
 
    A = [ 4, 2, 15, 9, 8, 8]
    B = [ 3, 4, 22 ]
 
    countEvenBit(A, B, 6, 3)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function that count the XOR of []B
// with all the element in []A having
// even set bit
static void countEvenBit(int []A, int []B, int n, int m)
{
    int i, cntOdd = 0, cntEven = 0;
    for (i = 0; i < n; i++)
    {
 
        // Count the set bits in A[i]
        int x = bitCount(A[i]);
 
        // check for even or Odd
        if (x % 2 == 1) {
            cntEven++;
        }
        else {
            cntOdd++;
        }
    }
 
    // To store the count of element for
    // []B such that XOR with all the
    // element in []A having even set bit
    int []CountB = new int[m];
 
    for (i = 0; i < m; i++) {
 
        // Count set bit for B[i]
        int x = bitCount(B[i]);
 
        // check for Even or Odd
        if (x % 2 == 1) {
            CountB[i] = cntEven;
        }
        else {
            CountB[i] = cntOdd;
        }
    }
 
    for (i = 0; i < m; i++) {
        Console.Write(CountB[i] +" ");
    }
}
static int bitCount(int x)
{
    int setBits = 0;
    while (x != 0) {
        x = x & (x - 1);
        setBits++;
    }
    return setBits;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []A = { 4, 2, 15, 9, 8, 8 };
    int []B = { 3, 4, 22 };
 
    countEvenBit(A, B, 6, 3);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
2 4 4

时间复杂度: O(N + M),其中 N 和 M 分别是给定的两个数组的长度。

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