Make all array elements equal by replacing triplets with their Bitwise XOR
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:
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++
#include <bits/stdc++.h>
using namespace std;
void checkXOR( int arr[], int N)
{
if (N % 2 == 0) {
int xro = 0;
for ( int i = 0; i < N; i++) {
xro ^= arr[i];
}
if (xro != 0) {
cout << -1 << endl;
return ;
}
for ( int i = 0; i < N - 3; i += 2) {
cout << i << " " << i + 1
<< " " << i + 2 << endl;
}
for ( int i = 0; i < N - 3; i += 2) {
cout << i << " " << i + 1
<< " " << N - 1 << endl;
}
}
else {
for ( int i = 0; i < N - 2; i += 2) {
cout << i << " " << i + 1 << " "
<< i + 2 << endl;
}
for ( int i = 0; i < N - 2; i += 2) {
cout << i << " " << i + 1
<< " " << N - 1 << endl;
}
}
}
int main()
{
int arr[] = { 4, 2, 1, 7, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
checkXOR(arr, N);
}
|
Java
import java.util.*;
class GFG{
static void checkXOR( int arr[], int N)
{
if (N % 2 == 0 )
{
int xro = 0 ;
for ( int i = 0 ; i < N; i++)
{
xro ^= arr[i];
}
if (xro != 0 )
{
System.out.println(- 1 );
return ;
}
for ( int i = 0 ; i < N - 3 ; i += 2 )
{
System.out.println(i + " " + (i + 1 ) +
" " + (i + 2 ));
}
for ( int i = 0 ; i < N - 3 ; i += 2 )
{
System.out.println(i + " " + (i + 1 ) +
" " + (N - 1 ));
}
}
else
{
for ( int i = 0 ; i < N - 2 ; i += 2 )
{
System.out.println(i + " " + (i + 1 ) +
" " + (i + 2 ));
}
for ( int i = 0 ; i < N - 2 ; i += 2 )
{
System.out.println(i + " " + (i + 1 ) +
" " + (N - 1 ));
}
}
}
public static void main(String[] args)
{
int arr[] = { 4 , 2 , 1 , 7 , 2 };
int N = arr.length;
checkXOR(arr, N);
}
}
|
Python3
def checkXOR(arr, N):
if (N % 2 = = 0 ):
xro = 0 ;
for i in range (N):
xro ^ = arr[i];
if (xro ! = 0 ):
print ( - 1 );
return ;
for i in range ( 0 , N - 3 , 2 ):
print (i, " " , (i + 1 ), " " , (i + 2 ), end = " " );
for i in range ( 0 , N - 3 , 2 ):
print (i, " " , (i + 1 ), " " , (N - 1 ), end = " " );
else :
for i in range ( 0 , N - 2 , 2 ):
print (i, " " , (i + 1 ), " " , (i + 2 ));
for i in range ( 0 , N - 2 , 2 ):
print (i, " " , (i + 1 ), " " , (N - 1 ));
if __name__ = = '__main__' :
arr = [ 4 , 2 , 1 , 7 , 2 ];
N = len (arr);
checkXOR(arr, N);
|
C#
using System;
class GFG{
static void checkXOR( int [] arr, int N)
{
if (N % 2 == 0)
{
int xro = 0;
for ( int i = 0; i < N; i++)
{
xro ^= arr[i];
}
if (xro != 0)
{
Console.WriteLine(-1);
return ;
}
for ( int i = 0; i < N - 3; i += 2)
{
Console.WriteLine(i + " " + (i + 1) +
" " + (i + 2));
}
for ( int i = 0; i < N - 3; i += 2)
{
Console.WriteLine(i + " " + (i + 1) +
" " + (N - 1));
}
}
else
{
for ( int i = 0; i < N - 2; i += 2)
{
Console.WriteLine(i + " " + (i + 1) +
" " + (i + 2));
}
for ( int i = 0; i < N - 2; i += 2)
{
Console.WriteLine(i + " " + (i + 1) +
" " + (N - 1));
}
}
}
public static void Main()
{
int [] arr = { 4, 2, 1, 7, 2 };
int N = arr.Length;
checkXOR(arr, N);
}
}
|
Javascript
<script>
function checkXOR(arr, N)
{
if (N % 2 == 0) {
let xro = 0;
for (let i = 0; i < N; i++) {
xro ^= arr[i];
}
if (xro != 0) {
document.write(-1 + "<br>" );
return ;
}
for (let i = 0; i < N - 3; i += 2) {
document.write(i + " " + (i + 1)
+ " " + (i + 2) + "<br>" );
}
for (let i = 0; i < N - 3; i += 2) {
document.write(i + " " + (i + 1)
+ " " + (N - 1) + "<br>" );
}
}
else {
for (let i = 0; i < N - 2; i += 2) {
document.write(i + " " + (i + 1) + " "
+ (i + 2) + "<br>" );
}
for (let i = 0; i < N - 2; i += 2) {
document.write(i + " " + (i + 1)
+ " " + (N - 1) + "<br>" );
}
}
}
let arr = [ 4, 2, 1, 7, 2 ];
let N = arr.length;
checkXOR(arr, N);
</script>
|
Output:
0 1 2
2 3 4
0 1 4
2 3 4
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...