Skip to content
Related Articles

Related Articles

Make all array elements equal by replacing triplets with their Bitwise XOR
  • Last Updated : 04 Jan, 2021

Given an array arr[] of size N, the task is to find all the triplets (i, j, k) such that replacing the elements of the triplets with their Bitwise XOR values, i.e. replacing arr[i], arr[j], arr[k] with (arr[i] ^ arr[j] ^ arr[k]) makes all array elements equal. If more than one solution exists, print any of them. Otherwise, print -1.

Examples:

Input: arr[] = { 4, 2, 1, 7, 2 } 
Output: { (0, 1, 2), (2, 3, 4), (0, 1, 4) } 
Explanation: 
Selecting a triplet (0, 1, 2) and replacing them with arr[0] ^ arr[1] ^ arr[2] modifies arr[] to { 7, 7, 7, 7, 2 } 
Selecting a triplet (2, 3, 4) and replacing them with arr[2] ^ arr[3] ^ arr[4] modifies arr[] to { 7, 7, 2, 2, 2 } 
Selecting a triplet (0, 1, 4) and replacing them with arr[0] ^ arr[1] ^ arr[2] modifies arr[] to { 2, 2, 2, 2, 2 }

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

 

Approach: The problem can be solved based on the following observation:



x ^ X ^ Y = Y 
X ^ Y ^ Y = X 
If any two elements of a triplet are equal, then replacing all the elements of the triplet with their Bitwise XOR makes all elements of the triplet equal to the third element of the triplet. 
 

Follow the steps below to solve the problem:

  • Selecting the triplets of the form { (0, 1, 2), (2, 3, 4) …} makes the elements of the pairs { (arr[0], arr[1]), (arr[2], arr[3])… } equal.
  • From the above observations, selecting the triplets of the form { (0, 1, N – 1), (2, 3, N -1), … } make all the array elements equal to the last element of the array.
  • Finally, print the triplets.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
 
// Function to find triplets such that
// replacing them with their XOR make
// all array elements equal
void checkXOR(int arr[], int N)
{
    // If N is even
    if (N % 2 == 0) {
     
        // Calculate xor of
        // array elements
        int xro = 0;
         
         
        // Traverse the array
        for (int i = 0; i < N; i++) {
             
            // Update xor
            xro ^= arr[i];
        }
 
        // If xor is not equal to 0
        if (xro != 0) {
            cout << -1 << endl;
            return;
        }
         
         
        // Selecting the triplets such that
        // elements of the pairs (arr[0], arr[1]),
        // (arr[2], arr[3])... can be made equal
        for (int i = 0; i < N - 3; i += 2) {
            cout << i << " " << i + 1
                 << " " << i + 2 << endl;
        }
 
        // Selecting the triplets such that
        // all array elements can be made
        // equal to arr[N - 1]
        for (int i = 0; i < N - 3; i += 2) {
            cout << i << " " << i + 1
                 << " " << N - 1 << endl;
        }
    }
    else {
 
        // Selecting the triplets such that
        // elements of the pairs (arr[0], arr[1]),
        // (arr[2], arr[3])... can be made equal
        for (int i = 0; i < N - 2; i += 2) {
            cout << i << " " << i + 1 << " "
                 << i + 2 << endl;
        }
         
         
         
        // Selecting the triplets such that
        // all array elements can be made
        // equal to arr[N - 1]
        for (int i = 0; i < N - 2; i += 2) {
            cout << i << " " << i + 1
                   << " " << N - 1 << endl;
        }
    }
}
 
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 4, 2, 1, 7, 2 };
 
    // Size of array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    checkXOR(arr, N);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.util.*;
  
class GFG{
      
// Function to find triplets such that
// replacing them with their XOR make
// all array elements equal
static void checkXOR(int arr[], int N)
{
     
    // If N is even
    if (N % 2 == 0)
    {
         
        // Calculate xor of
        // array elements
        int xro = 0;
         
        // Traverse the array
        for(int i = 0; i < N; i++)
        {
             
            // Update xor
            xro ^= arr[i];
        }
  
        // If xor is not equal to 0
        if (xro != 0)
        {
            System.out.println(-1);
            return;
        }
         
        // Selecting the triplets such that
        // elements of the pairs (arr[0], arr[1]),
        // (arr[2], arr[3])... can be made equal
        for(int i = 0; i < N - 3; i += 2)
        {
            System.out.println(i + " " + (i + 1) +
                                   " " + (i + 2));
        }
  
        // Selecting the triplets such that
        // all array elements can be made
        // equal to arr[N - 1]
        for(int i = 0; i < N - 3; i += 2)
        {
            System.out.println(i + " " + (i + 1) +
                                   " " + (N - 1));
        }
    }
    else
    {
         
        // Selecting the triplets such that
        // elements of the pairs (arr[0], arr[1]),
        // (arr[2], arr[3])... can be made equal
        for(int i = 0; i < N - 2; i += 2)
        {
            System.out.println(i + " " + (i + 1) +
                                   " " + (i + 2));
        }
         
        // Selecting the triplets such that
        // all array elements can be made
        // equal to arr[N - 1]
        for(int i = 0; i < N - 2; i += 2)
        {
            System.out.println(i + " " + (i + 1) +
                                   " " + (N - 1));
        }
    }
}
  
// Driver code
public static void main(String[] args)
{
     
    // Given array
    int arr[] = { 4, 2, 1, 7, 2 };
  
    // Size of array
    int N = arr.length;
  
    // Function call
    checkXOR(arr, N);
}
}
 
// This code is contributed by susmitakundugoaldanga

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to implement
# the above approach
 
# Function to find triplets such that
# replacing them with their XOR make
# all array elements equal
def checkXOR(arr, N):
   
    # If N is even
    if (N % 2 == 0):
 
        # Calculate xor of
        # array elements
        xro = 0;
 
        # Traverse the array
        for i in range(N):
           
            # Update xor
            xro ^= arr[i];
 
        # If xor is not equal to 0
        if (xro != 0):
            print(-1);
            return;
 
        # Selecting the triplets such that
        # elements of the pairs (arr[0], arr[1]),
        # (arr[2], arr[3])... can be made equal
        for i in range(0, N - 3, 2):
            print(i, " ", (i + 1), " ", (i + 2), end=" ");
 
        # Selecting the triplets such that
        # all array elements can be made
        # equal to arr[N - 1]
        for i in range(0, N - 3, 2):
            print(i, " ", (i + 1), " ", (N - 1), end=" ");
 
    else:
 
        # Selecting the triplets such that
        # elements of the pairs (arr[0], arr[1]),
        # (arr[2], arr[3])... can be made equal
        for i in range(0, N - 2, 2):
            print(i, " ", (i + 1), " ", (i + 2));
 
        # Selecting the triplets such that
        # all array elements can be made
        # equal to arr[N - 1]
        for i in range(0, N - 2, 2):
            print(i, " ", (i + 1), " ", (N - 1));
 
 
# Driver code
if __name__ == '__main__':
   
    # Given array
    arr = [4, 2, 1, 7, 2];
 
    # Size of array
    N = len(arr);
 
    # Function call
    checkXOR(arr, N);
 
# This code is contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach 
using System;
   
class GFG{
       
// Function to find triplets such that
// replacing them with their XOR make
// all array elements equal
static void checkXOR(int[] arr, int N)
{
     
    // If N is even
    if (N % 2 == 0)
    {
         
        // Calculate xor of
        // array elements
        int xro = 0;
          
        // Traverse the array
        for(int i = 0; i < N; i++)
        {
             
            // Update xor
            xro ^= arr[i];
        }
   
        // If xor is not equal to 0
        if (xro != 0)
        {
            Console.WriteLine(-1);
            return;
        }
          
        // Selecting the triplets such that
        // elements of the pairs (arr[0], arr[1]),
        // (arr[2], arr[3])... can be made equal
        for(int i = 0; i < N - 3; i += 2)
        {
            Console.WriteLine(i + " " + (i + 1) +
                                  " " + (i + 2));
        }
   
        // Selecting the triplets such that
        // all array elements can be made
        // equal to arr[N - 1]
        for(int i = 0; i < N - 3; i += 2)
        {
            Console.WriteLine(i + " " + (i + 1) +
                                  " " + (N - 1));
        }
    }
    else
    {
          
        // Selecting the triplets such that
        // elements of the pairs (arr[0], arr[1]),
        // (arr[2], arr[3])... can be made equal
        for(int i = 0; i < N - 2; i += 2)
        {
            Console.WriteLine(i + " " + (i + 1) +
                                  " " + (i + 2));
        }
          
        // Selecting the triplets such that
        // all array elements can be made
        // equal to arr[N - 1]
        for(int i = 0; i < N - 2; i += 2)
        {
            Console.WriteLine(i + " " + (i + 1) +
                                  " " + (N - 1));
        }
    }
}
   
// Driver code
public static void Main()
{
     
    // Given array
    int[] arr = { 4, 2, 1, 7, 2 };
   
    // Size of array
    int N = arr.Length;
   
    // Function call
    checkXOR(arr, N);
}
}
 
// This code is contributed by sanjoy_62

chevron_right


 
 

Output: 

0 1 2
2 3 4
0 1 4
2 3 4

 

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 :