XOR of every element of an Array with a given number K

13 Mar, 2022
Given an array arr and a number K, find the new array formed by performing XOR of the corresponding element from the given array with the given number K.Examples:  
Input: arr[] = { 2, 4, 1, 3, 5 }, K = 5 Output: 7 1 4 6 0 Explanation: 2 XOR 5 = 7 4 XOR 5 = 1 1 XOR 5 = 4 3 XOR 5 = 6 5 XOR 5 = 0 Input: arr[] = { 4, 75, 45, 42 }, K = 2 Output: 6 73 47 40  
 
Approach: 
Traverse the given array.Then calculate the XOR of each element with K.Then store it as the element at that index in the output array.Print the updated array.
Traverse the given array.
Then calculate the XOR of each element with K.
Then store it as the element at that index in the output array.
Print the updated array.
Below is the implementation of the above approach. 
CPP
Java
Python3
C#
Javascript
// C++ program to find XOR of every element// of an array with a given number K #include <bits/stdc++.h>using namespace std; // Function to construct new arrayvoid constructXORArray(int A[], int n, int K){    int B[n];     // Traverse the array and    // compute XOR with K    for (int i = 0; i < n; i++)        B[i] = A[i] ^ K;     // Print new array    for (int i = 0; i < n; i++)        cout << B[i] << " ";    cout << endl;} // Driver codeint main(){    int A[] = { 2, 4, 1, 3, 5 };    int K = 5;    int n = sizeof(A) / sizeof(A[0]);    constructXORArray(A, n, K);     int B[] = { 4, 75, 45, 42 };    K = 2;    n = sizeof(B) / sizeof(B[0]);    constructXORArray(B, n, K);     return 0;}
// Java program to find XOR of every element// of an array with a given number Kimport java.util.*;class GFG{ // Function to construct new arraystatic void constructXORArray(int A[], int n, int K){           int[] B = new int[n];     // Traverse the array and    // compute XOR with K    for (int i = 0; i < n; i++)        B[i] = A[i] ^ K;     // Print new array    for (int i = 0; i < n; i++)        System.out.print( B[i] +" ");    System.out.println();} // Driver codepublic 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); }} // This code is contributed by shivanisinghss2110
# Python program to find XOR of every element# of an array with a given number K # Function to construct new arraydef constructXORArray(A, n, K):     B = [0]*n;     # Traverse the array and    # compute XOR with K    for i in range(n):        B[i] = A[i] ^ K;     # Print new array    for i in range(n):        print(B[i], end=" ");    print();  # Driver codeif __name__ == '__main__':    A = [ 2, 4, 1, 3, 5 ];    K = 5;    n = len(A);    constructXORArray(A, n, K);     B = [ 4, 75, 45, 42 ];    K = 2;    n = len(B);    constructXORArray(B, n, K);# This code contributed by sapnasingh4991
// C# program to find XOR of every element// of an array with a given number Kusing System; class GFG{  // Function to construct new arraystatic void constructXORArray(int []A, int n, int K){            int[] B = new int[n];      // Traverse the array and    // compute XOR with K    for (int i = 0; i < n; i++)        B[i] = A[i] ^ K;      // Print new array    for (int i = 0; i < n; i++)        Console.Write( B[i] +" ");    Console.WriteLine();}  // Driver codepublic 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);}} // This code is contributed by Rajput-Ji
<script>    // Javascript program to find XOR of every element    // of an array with a given number K         // Function to construct new array    function constructXORArray(A, n, K)    {        let B = new Array(n);         // Traverse the array and        // compute XOR with K        for (let i = 0; i < n; i++)            B[i] = A[i] ^ K;         // Print new array        for (let i = 0; i < n; i++)            document.write(B[i] + " ");        document.write("</br>")    }         let A = [ 2, 4, 1, 3, 5 ];    let K = 5;    let n = A.length;    constructXORArray(A, n, K);       let B = [ 4, 75, 45, 42 ];    K = 2;    n = B.length;    constructXORArray(B, n, K);         // This code is contributed by divyeshrabadiya07.</script>
7 1 4 6 0 
6 73 47 40
 
Time Complexity: O(n)Auxiliary Space: O(n)
shivanisinghss2110
Rajput-Ji
sapnasingh4991
divyeshrabadiya07
singhh3010
Bitwise-XOR
Arrays
Bit Magic
School Programming
Arrays
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Data Structures
Window Sliding Technique
Search, insert and delete in an unsorted array
What is Data Structure: Types, Classifications and Applications
Chocolate Distribution Problem
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
How to swap two numbers without using a temporary variable?