📌  相关文章
📜  使用奇数按位异或计算对,可以将其删除并替换为按位或

📅  最后修改于: 2021-09-07 02:09:09             🧑  作者: Mango

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

例子:

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

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

因此,这个想法是找到给定数组中存在的偶数元素的计数。如果偶数元素的计数为N ,则需要0 次移动。否则,打印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)

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