The question is to find XOR of the XOR’s of all subsets. i.e if the set is {1,2,3}. All subsets are : [{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}]. Find the XOR of each of the subset and then find the XOR of every subset result.
We strongly recommend you to minimize your browser and try this yourself first.
This is a very simple question to solve if we get the first step (and only step) right. The solution is XOR is always 0 when n > 1 and Set[0] when n is 1.
C++
#include <bits/stdc++.h>
using namespace std;
int findXOR( int Set[], int n)
{
if (n == 1)
return Set[0];
else
return 0;
}
int main()
{
int Set[] = { 1, 2, 3 };
int n = sizeof (Set) / sizeof (Set[0]);
cout << "XOR of XOR's of all subsets is "
<< findXOR(Set, n);
return 0;
}
|
C
#include <stdio.h>
int findXOR( int Set[], int n)
{
if (n == 1)
return Set[0];
else
return 0;
}
int main()
{
int Set[] = { 1, 2, 3 };
int n = sizeof (Set) / sizeof (Set[0]);
printf ( "XOR of XORs of all subsets is %d\n" , findXOR(Set, n));
return 0;
}
|
Java
import java.util.*;
class GFG {
static int findXOR( int Set[], int n)
{
if (n == 1 )
return Set[ 0 ];
else
return 0 ;
}
public static void main(String arg[])
{
int Set[] = { 1 , 2 , 3 };
int n = Set.length;
System.out.print( "XOR of XOR's of all subsets is "
+ findXOR(Set, n));
}
}
|
Python3
def findXOR( Set , n):
if (n = = 1 ):
return Set [ 0 ]
else :
return 0
Set = [ 1 , 2 , 3 ]
n = len ( Set )
print ( "XOR of XOR's of all subsets is " ,
findXOR( Set , n))
|
C#
using System;
class GFG {
static int findXOR( int [] Set, int n)
{
if (n == 1)
return Set[0];
else
return 0;
}
public static void Main()
{
int [] Set = { 1, 2, 3 };
int n = Set.Length;
Console.Write( "XOR of XOR's of all subsets is "
+ findXOR(Set, n));
}
}
|
PHP
<?php
function findXOR( $Set , $n )
{
if ( $n == 1)
return $Set [0];
else
return 0;
}
$Set = array (1, 2, 3);
$n = count ( $Set );
echo "XOR of XOR's of all subsets is "
, findXOR( $Set , $n );
?>
|
Javascript
<script>
function findXOR(Set, n)
{
if (n == 1)
return Set[0];
else
return 0;
}
let Set = [1, 2, 3];
let n = Set.length;
document.write( "XOR of XOR's of all subsets is "
+ findXOR(Set, n));
</script>
|
Output:
XOR of XOR's of all subsets is 0
Time Complexity: O(1)
Auxiliary Space: O(1)
Related Problem :
Sum of XOR of all possible subsets
How does this work?
The logic goes simple. When we apply XOR on all the subsets of a set, we can use the commutative and associative property of XOR which reduces the problem to finding XOR result of each element that depends on the total number of occurrences of each element. Eg. XOR([{1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}]) = XOR(XOR(1,1,1,1), XOR(2,2,2,2), XOR(3,3,3,3), XOR(4,4,4,4))
Let us consider n’th element, it can be included in the power set of remaining (n-1) elements. The number of subsets for (n-1) elements is equal to 2(n-1) which is always even when n>1. Thus, in the XOR result, every element is included even number of times and XOR of even occurrences of any number is 0.
This article is contributed by Ekta Goel. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...