Pairs with Given XOR solution
Idea :
a^b=cit means b^c=a
so we will check for every element and if xor is there in set then increment count.
Code:
public static int xorpair(int arr[],int n,int x)
{
Set<Integer> h=new HashSet<Integer>();
int c=0;
for(int t:arr)
{
if(h.contains(t^x))
++c;
h.add(t);
}
return c;
}
0 Comments:
Post a Comment