# Make all array elements equal by replacing triplets with their Bitwise XOR

• Difficulty Level : Medium
• Last Updated : 30 Apr, 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:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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 ^ arr ^ arr modifies arr[] to { 7, 7, 7, 7, 2 }
Selecting a triplet (2, 3, 4) and replacing them with arr ^ arr ^ arr modifies arr[] to { 7, 7, 2, 2, 2 }
Selecting a triplet (0, 1, 4) and replacing them with arr ^ arr ^ arr 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, arr), (arr, arr)… } 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++

 `// C++ program to implement``// the above approach` `#include ``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, arr),``        ``// (arr, arr)... 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, arr),``        ``// (arr, arr)... 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);` `    ``// Function call``    ``checkXOR(arr, N);``}`

## Java

 `// 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, arr),``        ``// (arr, arr)... 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, arr),``        ``// (arr, arr)... 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`

## Python3

 `# 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, arr),``        ``# (arr, arr)... 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, arr),``        ``# (arr, arr)... 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`

## C#

 `// 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, arr),``        ``// (arr, arr)... 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, arr),``        ``// (arr, arr)... 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`

## Javascript

 ``

Output:
```0 1 2
2 3 4
0 1 4
2 3 4```

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

My Personal Notes arrow_drop_up