Make all array elements equal by replacing triplets with their Bitwise XOR | Java program to implement the above approach ; Function to find triplets such that replacing them with their XOR make all array elements equal ; If N is even ; Calculate xor of array elements ; Traverse the array ; Update xor ; If xor is not equal to 0 ; Selecting the triplets such that elements of the pairs ( arr [ 0 ] , arr [ 1 ] ) , ( arr [ 2 ] , arr [ 3 ] ) . . . can be made equal ; Selecting the triplets such that all array elements can be made equal to arr [ N - 1 ] ; Selecting the triplets such that elements of the pairs ( arr [ 0 ] , arr [ 1 ] ) , ( arr [ 2 ] , arr [ 3 ] ) . . . can be made equal ; Selecting the triplets such that all array elements can be made equal to arr [ N - 1 ] ; Driver code ; Given array ; Size of array ; Function call
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 ) ; } }