Skip to content
Related Articles

Related Articles

Maximize count of pairs whose bitwise XOR is even by replacing such pairs with their Bitwise XOR
  • Last Updated : 03 Mar, 2021

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: 3
Explanation:
Step 1: Remove the pair (4, 6) and replace them by their XOR value (= 2) in the array. Therefore, the array arr[] modifies to {2, 1, 3}.
Step 2: Remove the pair (1, 3) and replace them by their XOR value (= 2) in the array, modifies the array as arr[] = {2, 2}.
At last select the pair (2, 2) and then remove the pair and insert the xor of 2 and 2 in the array which modifies the array as arr[] ={0}.
Now no other pair can be chosen therefore 3 is the maximum number of pairs whose Xor is even.

Input: arr[ ] = { 1, 2, 3, 4, 5 }
Output: 3

Naive Approach: The simplest approach to solve this problem is to find all possible pairs of the array and for each pair, check if their Bitwise XOR is even or not. If found to be true, then increment the count of pairs and remove both the elements from the array and add their XOR to the array. Repeat the above steps until no more pairs can be selected. Print the count of operations performed. 
Time Complexity: O(N3)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized based on the following observations:



  • Even ^ Even = Even
  • Odd ^ Odd = Even
  • The total number of pairs that can be formed from only odd numbers satisfying the conditions is odd / 2.
  • The total numbers of pairs that can be formed from only even numbers satisfying the conditions is even – 1.

Follow the steps below to solve the problem:

  1. Traverse the array.
  2. Count the frequency of odd numbers and store it in a variable, say odd.
  3. The total number of pairs with even XOR that can be formed from all the odd array elements is floor(odd / 2).
  4. Deleting the formed pairs in the above step and replacing them with their XOR values respectively, increases the count of even elements by floor(odd / 2).
  5. Finally, print the count of pairs that can be formed with even XOR as (N – odd + odd/2 -1) + odd / 2.

Below is the implementation of the above approach:

C++




// C++ program to implement the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to maximize the count
// of pairs with even XOR possible
// in an array by given operations
int countPairs(int arr[], int N)
{
    // Stores count of odd
    // array elements
    int odd = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // If arr[i] is odd
        if (arr[i] & 1)
            odd++;
    }
 
    // Stores the total number
    // of even pairs
    int ans = (N - odd + odd / 2
               - 1)
              + odd / 2;
 
    return ans;
}
 
// Driver Code
int main()
{
    // Input
    int arr[] = { 4, 6, 1, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call to count the number
    // of pairs whose XOR is even
    cout << countPairs(arr, N);
 
    return 0;
}


Java




// Java program to implement the above approach
public class GFG
{
 
  // Function to maximize the count
  // of pairs with even XOR possible
  // in an array by given operations
  static int countPairs(int []arr, int N)
  {
 
    // Stores count of odd
    // array elements
    int odd = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
      // If arr[i] is odd
      if ((arr[i] & 1)!=0)
        odd++;
    }
 
    // Stores the total number
    // of even pairs
    int ans = (N - odd + odd / 2
               - 1)
      + odd / 2;
 
    return ans;
  }
 
  // Driver Code
  public static void main(String args[])
  {
 
    // Input
    int []arr = { 4, 6, 1, 3 };
    int N = arr.length;
 
    // Function call to count the number
    // of pairs whose XOR is even
    System.out.println(countPairs(arr, N));
  }
}
 
// This code is contributed by AnkThon.


Python3




# Python3 program to implement the above approach
 
# Function to maximize the count
# of pairs with even XOR possible
# in an array by given operations
def countPairs(arr, N):
   
    # Stores count of odd
    # array elements
    odd = 0
 
    # Traverse the array
    for i in range(N):
 
        # If arr[i] is odd
        if (arr[i] & 1):
            odd += 1
 
    # Stores the total number
    # of even pairs
    ans = (N - odd + odd // 2 - 1) + odd // 2
 
    return ans
 
# Driver Code
if __name__ == '__main__':
   
  # Input
    arr =[4, 6, 1, 3]
    N = len(arr)
 
    # Function call to count the number
    # of pairs whose XOR is even
    print (countPairs(arr, N))
 
    # This code is contributed by mohit kumar 29.


C#




// C# program to implement the above approach
using System;
class GFG
{
 
// Function to maximize the count
// of pairs with even XOR possible
// in an array by given operations
static int countPairs(int []arr, int N)
{
   
    // Stores count of odd
    // array elements
    int odd = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++)
    {
 
        // If arr[i] is odd
        if ((arr[i] & 1)!=0)
            odd++;
    }
 
    // Stores the total number
    // of even pairs
    int ans = (N - odd + odd / 2
               - 1)
              + odd / 2;
 
    return ans;
}
 
// Driver Code
public static void Main()
{
   
    // Input
    int []arr = { 4, 6, 1, 3 };
    int N = arr.Length;
 
    // Function call to count the number
    // of pairs whose XOR is even
    Console.Write(countPairs(arr, N));
}
}
 
// This code is contributed by SURENDRA_GANGWAR.


 
 

Output: 

3

 

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :