Saturday, 23 Oct 2021

# Count pairs with odd Bitwise XOR that can be removed and replaced by their Bitwise OR

Count pairs with odd Bitwise XOR that can be removed and replaced by their Bitwise ORGiven an array arr[] consisting of N integers, the task is to count the number of pairs whose Bitwise XOR is odd, that can be removed and replaced by their Bitwise OR values until no such pair exists in the array.Examples:Input: arr[] = {5, 4, 7, 2}Output: 2Explanation:Pair (5, 4): Bitwise XOR of 5 and 4 is 1. Remove this pair and add their Bitwise OR (= 5) into the array. Therefore, the modified array is {5, 7, 2}.Pair (5, 2): Bitwise XOR of 5 and 2 is 7. Remove this pair and add their Bitwise OR (= 7) into the array. Therefore, the modified array is {7, 7}.Therefore, the count of such pairs that can be removed is 2.Input: arr[] = {2, 4, 6}Output: 0Approach: The given problem can be solved based on the following observations:Therefore, the idea is to find the count of even elements present in the given array. If the count of even elements is N, then 0 moves are required. Otherwise, print the value of count as the resultant count of pairs required to be removed.Below is the implementation of the above approach:C++#include using namespace std;void countPairs(int arr[], int N){            int even = 0;        for (int i = 0; i < N; i++) {                        if (arr[i] % 2 == 0)            even++;    }            if (N – even >= 1) {        cout = 1)    {        Console.WriteLine(even);        return;    }        Console.WriteLine(0);}static void Main(){    int[] arr = { 5, 4, 7, 2 };    int N = arr.Length;    countPairs(arr, N);}}Output: 2Time Complexity: O(N)Auxiliary Space: O(1)