XOR of all elements except i
Step 1: Idea is to take XOR of all the elements of array .
Step 2: Again we will xor with element at index i i.e. when we xor element twice(first & second step) it will make original
Code:
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
int n=ab.nextInt();
int arr[]=new int[n];
int xor=0;
for(int i=0;i<n;i++)
{ arr[i]=ab.nextInt();
xor^=arr[i];
}
for(int i=0;i<n;i++)
System.out.print((xor^arr[i])+" ");
System.out.println();
}
}
Step 2: Again we will xor with element at index i i.e. when we xor element twice(first & second step) it will make original
Code:
public static void main (String[] args)
{
Scanner ab=new Scanner(System.in);
int t=ab.nextInt();
while(t-->0)
{
int n=ab.nextInt();
int arr[]=new int[n];
int xor=0;
for(int i=0;i<n;i++)
{ arr[i]=ab.nextInt();
xor^=arr[i];
}
for(int i=0;i<n;i++)
System.out.print((xor^arr[i])+" ");
System.out.println();
}
}
0 Comments:
Post a Comment