📌  相关文章
📜  X查询与K进行XOR后的偶数和奇数置位数组元素的计数

📅  最后修改于: 2021-06-26 20:39:41             🧑  作者: Mango

给定N个元素的数组arr和另一个包含K值的数组Q ,任务是在数组arr中与数组Q中的每个元素K进行异或运算后,打印出奇数和偶数置位的数组arr中的元素计数。

例子:

方法:

  • 两个均具有奇数或偶数置位的元素的异或结果将导致偶数置位。
  • 两个元素的异或结果为奇数位,一个为奇数,另一个为偶数,反之亦然。
  • 使用Brian Kernighan的算法预先计算所有数组元素的偶数和奇数位的元素计数。
  • 对于Q的所有元素,计数置位位数。如果设置位的计数为偶数,则偶数和奇数设置位元素的计数保持不变。否则,反转计数并显示。

下面是上述方法的实现:

C++
// C++ Program to count number
// of even and odd set bits
// elements after XOR with a
// given element
 
#include 
using namespace std;
 
void keep_count(int arr[], int& even,
                int& odd, int N)
{
    // Store the count of set bits
    int count;
    for (int i = 0; i < N; i++) {
        count = 0;
 
        // Brian Kernighan's algorithm
        while (arr[i] != 0) {
            arr[i] = (arr[i] - 1) & arr[i];
            count++;
        }
 
        if (count % 2 == 0)
            even++;
        else
            odd++;
    }
 
    return;
}
 
// Function to solve Q queries
void solveQueries(
    int arr[], int n,
    int q[], int m)
{
 
    int even_count = 0, odd_count = 0;
 
    keep_count(arr, even_count,
               odd_count, n);
 
    for (int i = 0; i < m; i++) {
 
        int X = q[i];
 
        // Store set bits in X
        int count = 0;
 
        // Count set bits of X
        while (X != 0) {
            X = (X - 1) & X;
            count++;
        }
 
        if (count % 2 == 0) {
            cout << even_count << " "
                 << odd_count << "\n";
        }
        else {
            cout << odd_count << " "
                 << even_count
                 << "\n";
        }
    }
}
 
// Driver code
int main()
{
    int arr[] = { 2, 7, 4, 5, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    int q[] = { 3, 4, 12, 6 };
    int m = sizeof(q) / sizeof(q[0]);
 
    solveQueries(arr, n, q, m);
 
    return 0;
}


Java
// Java program to count number
// of even and odd set bits
// elements after XOR with a
// given element
class GFG{
     
static int even, odd;
 
static void keep_count(int arr[], int N)
{
     
    // Store the count of set bits
    int count;
     
    for(int i = 0; i < N; i++)
    {
       count = 0;
        
       // Brian Kernighan's algorithm
       while (arr[i] != 0)
       {
           arr[i] = (arr[i] - 1) & arr[i];
           count++;
       }
       if (count % 2 == 0)
           even++;
       else
           odd++;
    }
    return;
}
 
// Function to solve Q queries
static void solveQueries(int arr[], int n,
                         int q[], int m)
{
    even = 0;
    odd = 0;
    keep_count(arr, n);
 
    for(int i = 0; i < m; i++)
    {
       int X = q[i];
        
       // Store set bits in X
       int count = 0;
        
       // Count set bits of X
       while (X != 0)
       {
           X = (X - 1) & X;
           count++;
       }
       if (count % 2 == 0)
       {
           System.out.print(even + " " +
                             odd + "\n");
       }
       else
       {
           System.out.print(odd + " " +
                           even + "\n");
       }
    }
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 7, 4, 5, 3 };
    int n = arr.length;
 
    int q[] = { 3, 4, 12, 6 };
    int m = q.length;
 
    solveQueries(arr, n, q, m);
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program to count number
# of even and odd set bits
# elements after XOR with a
# given element
 
even = 0
odd = 0
 
def keep_count(arr, N):
     
    global even
    global odd
     
    # Store the count of set bits
    for i in range(N):
        count = 0
 
        # Brian Kernighan's algorithm
        while (arr[i] != 0):
            arr[i] = (arr[i] - 1) & arr[i]
            count += 1
 
        if (count % 2 == 0):
            even += 1
        else:
            odd += 1
 
    return
 
# Function to solve Q queries
def solveQueries(arr, n, q, m):
     
    global even
    global odd
     
    keep_count(arr, n)
 
    for i in range(m):
        X = q[i]
 
        # Store set bits in X
        count = 0
 
        # Count set bits of X
        while (X != 0):
            X = (X - 1) & X
            count += 1
 
        if (count % 2 == 0):
            print(even, odd)
        else:
            print(odd, even)
 
# Driver code
if __name__ == '__main__':
     
    arr = [ 2, 7, 4, 5, 3 ]
    n = len(arr)
 
    q = [ 3, 4, 12, 6 ]
    m = len(q)
 
    solveQueries(arr, n, q, m)
 
# This code is contributed by samarth


C#
// C# program to count number
// of even and odd set bits
// elements after XOR with a
// given element
using System;
class GFG{
     
static int even, odd;
 
static void keep_count(int []arr, int N)
{
     
    // Store the count of set bits
    int count;
     
    for(int i = 0; i < N; i++)
    {
        count = 0;
             
        // Brian Kernighan's algorithm
        while (arr[i] != 0)
        {
            arr[i] = (arr[i] - 1) & arr[i];
            count++;
        }
        if (count % 2 == 0)
            even++;
        else
            odd++;
    }
    return;
}
 
// Function to solve Q queries
static void solveQueries(int []arr, int n,
                         int []q, int m)
{
    even = 0;
    odd = 0;
    keep_count(arr, n);
 
    for(int i = 0; i < m; i++)
    {
        int X = q[i];
             
        // Store set bits in X
        int count = 0;
             
        // Count set bits of X
        while (X != 0)
        {
            X = (X - 1) & X;
            count++;
        }
        if (count % 2 == 0)
        {
            Console.Write(even + " " +
                           odd + "\n");
        }
        else
        {
            Console.Write(odd + " " +
                         even + "\n");
        }
    }
}
 
// Driver code
public static void Main()
{
    int []arr = { 2, 7, 4, 5, 3 };
    int n = arr.Length;
 
    int []q = { 3, 4, 12, 6 };
    int m = q.Length;
 
    solveQueries(arr, n, q, m);
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
2 3
3 2
2 3
2 3

时间复杂度: O(N * log N)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。