📌  相关文章
📜  对具有奇数按位XOR的计数对,可以将其删除并用其按位OR替换

📅  最后修改于: 2021-05-14 02:08:12             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是计算其按位XOR为奇数的对数,可以将其删除并用其按位或值替换,直到数组中不存在这样的对。

例子:

方法:可以根据以下观察结果解决给定问题:

  • 一对按位XOR是奇数仅当一个元件是奇数,而另一个是偶数
  • 因此,从数组中删除这样的对并将其按位或添加到数组中不会影响数组中奇数元素的总数。但是偶数数组元素的数量减少1

因此,想法是找到给定数组中偶数元素的数量。如果偶数元素的数量为N ,则需要移动0 。否则,将count的值打印为需要删除的成对的结果count。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to count the number of pairs
// required to be removed from the array
// and replaced by their Bitwise OR values
void countPairs(int arr[], int N)
{
    // Stores the count of
    // even array elements
    int even = 0;
 
    // Traverse the given array
    for (int i = 0; i < N; i++) {
 
        // Increment the count
        // of even array elements
        if (arr[i] % 2 == 0)
            even++;
    }
 
    // If the array contains at
    // least one odd array element
    if (N - even >= 1) {
        cout << even;
        return;
    }
 
    // Otherwise, print 0
    cout << 0;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 4, 7, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    countPairs(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to count the number of pairs
// required to be removed from the array
// and replaced by their Bitwise OR values
static void countPairs(int arr[], int N)
{
     
    // Stores the count of
    // even array elements
    int even = 0;
 
    // Traverse the given array
    for(int i = 0; i < N; i++)
    {
         
        // Increment the count
        // of even array elements
        if (arr[i] % 2 == 0)
            even++;
    }
 
    // If the array contains at
    // least one odd array element
    if (N - even >= 1)
    {
        System.out.println(even);
        return;
    }
 
    // Otherwise, print 0
    System.out.println(0);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 5, 4, 7, 2 };
    int N = arr.length;
 
    countPairs(arr, N);
}
}
 
// This code is contributed by Kingash


Python3
# Python3 program for the above approach
 
# Function to count the number of pairs
# required to be removed from the array
# and replaced by their Bitwise OR values
def countPairs(arr, N):
 
    # Stores the count of
    # even array elements
    even = 0
 
    # Traverse the given array
    for i in range(N):
 
        # Increment the count
        # of even array elements
        if (arr[i] % 2 == 0):
            even += 1
     
    # If the array contains at
    # least one odd array element
    if (N - even >= 1):
        print(even)
        return
 
    # Otherwise, print 0
    print(0)
 
# Driver Code
if __name__ == "__main__":
 
    arr = [ 5, 4, 7, 2 ]
    N = len(arr)
     
    countPairs(arr, N)
 
# This code is contributed by AnkThon


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to count the number of pairs
// required to be removed from the array
// and replaced by their Bitwise OR values
static void countPairs(int[] arr, int N)
{
     
    // Stores the count of
    // even array elements
    int even = 0;
 
    // Traverse the given array
    for(int i = 0; i < N; i++)
    {
         
        // Increment the count
        // of even array elements
        if (arr[i] % 2 == 0)
            even++;
    }
 
    // If the array contains at
    // least one odd array element
    if (N - even >= 1)
    {
        Console.WriteLine(even);
        return;
    }
 
    // Otherwise, print 0
    Console.WriteLine(0);
}
 
// Driver code
static void Main()
{
    int[] arr = { 5, 4, 7, 2 };
    int N = arr.Length;
 
    countPairs(arr, N);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
2

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