XOR of every element of an Array with a given number K | Java program to find XOR of every element of an array with a given number K ; Function to construct new array ; Traverse the array and compute XOR with K ; Print new array ; Driver code
import java . util . * ; class GFG { static void constructXORArray ( int A [ ] , int n , int K ) { int [ ] B = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) B [ i ] = A [ i ] ^ K ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( B [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int A [ ] = { 2 , 4 , 1 , 3 , 5 } ; int K = 5 ; int n = A . length ; constructXORArray ( A , n , K ) ; int B [ ] = { 4 , 75 , 45 , 42 } ; K = 2 ; n = B . length ; constructXORArray ( B , n , K ) ; } }