Count pairs with odd Bitwise XOR that can be removed and replaced by their Bitwise OR
Last Updated :
06 Apr, 2021
Given 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: 2
Explanation:
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: 0
Approach: 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++
// C++ program for the above approach
#include <bits/stdc++.h>
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
<script>
// JavaScript 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
function countPairs(arr, N)
{
// Stores the count of
// even array elements
let even = 0;
// Traverse the given array
for (let 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) {
document.write(even);
return;
}
// Otherwise, print 0
document.write(0);
}
// Driver Code
let arr = [ 5, 4, 7, 2 ];
let N = arr.length;
countPairs(arr, N);
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Maximize count of pairs whose Bitwise AND exceeds Bitwise XOR by replacing such pairs with their Bitwise AND Given an array arr[] consisting of N positive integers, replace pairs of array elements whose Bitwise AND exceeds Bitwise XOR values by their Bitwise AND value. Finally, count the maximum number of such pairs that can be generated from the array. Examples: Input: arr[] = {12, 9, 15, 7}Output: 2Expla
9 min read
Maximize count of pairs whose bitwise XOR is even by replacing such pairs with their Bitwise XOR Given an array arr[] of size N, the task is to replace a pair of array elements whose Bitwise XOR is even by their Bitwise XOR. Repeat the above step as long as possible. Finally, print the count of such operations performed on the array Examples: Input: arr[] = { 4, 6, 1, 3 }Output: 3Explanation:St
11 min read
Count pairs with equal Bitwise AND and Bitwise OR value Given an array, arr[] of size N, the task is to count the number of unordered pairs such that Bitwise AND and Bitwise OR of each pair is equal. Examples: Input: arr[] = {1, 2, 1} Output: 1 Explanation: Bitwise AND value and Bitwise OR value all possible pairs are: Bitwise AND of the pair(arr[0], arr
6 min read
Count of pairs with bitwise XOR value greater than its bitwise AND value Given an array arr that contains N positive Integers. Find the count of all possible pairs whose bitwise XOR value is greater than bitwise AND value Examples: Input : arr[]={ 12, 4, 15}Output: 2Explanation: 12 ^ 4 = 8, 12 & 4 = 4. so 12 ^ 4 > 12 & 4 4 ^ 15 = 11, 4 & 15 = 4. so 4 ^ 15
4 min read
Count of pairs with bitwise XOR value greater than its bitwise AND value | Set 2 Given an array arr that contains N positive Integers. Find the count of all possible pairs whose bit wise XOR value is greater than bit wise AND value Examples: Input : arr[]={ 12, 4 , 15}Output: 2Explanation: 12 ^ 4 = 8 , 12 & 4 = 4 . so 12 ^ 4 > 12 & 4 4 ^ 15 = 11, 4 & 15 = 4. so 4
6 min read
Count pairs with bitwise XOR exceeding bitwise AND from a given array Given an array, arr[] of size N, the task is to count the number of pairs from the given array such that the bitwise AND(&) of each pair is less than its bitwise XOR(^). Examples: Input: arr[] = {1, 2, 3, 4, 5} Output: 8Explanation: Pairs that satisfy the given conditions are: (1 & 2) < (
10 min read