Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Sunday, October 1, 2017

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

Stats