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 = 0Input: 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.
Below is the implementation of the above approach.
CPP
// 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 array void 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 code int 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
// Java program to find XOR of every element // of an array with a given number K import java.util.*; class GFG { // Function to construct new array static 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 code 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); } } // This code is contributed by shivanisinghss2110 |
Python3
# Python program to find XOR of every element # of an array with a given number K # Function to construct new array def 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 code if __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#
// C# program to find XOR of every element // of an array with a given number K using System; class GFG { // Function to construct new array static 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 code 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); } } // This code is contributed by Rajput-Ji |
7 1 4 6 0 6 73 47 40
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Number of ways to change the Array such that largest element is LCM of array
- Nearest prime number in the array of every array element
- Find Array formed by adding each element of given array with largest element in new array to its left
- First element that appears even number of times in an array
- Maximum number of multiples in an array before any element
- Largest Divisor for each element in an array other than 1 and the number itself
- Minimum and Maximum element of an array which is divisible by a given number k
- Count the number of pop operations on stack to get each element of the array
- Smallest number to make Array sum at most K by dividing each element
- Largest element smaller than current element on left for every element in Array
- Count the number of elements which are greater than any of element on right side of an array
- Minimum number of given operations required to reduce the array to 0 element
- Number of ways to erase exactly one element in the Binary Array to make XOR zero
- Search an element in an unsorted array using minimum number of comparisons
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Remove minimum number of elements such that no common element exist in both array
- Form an array of distinct elements with each element as sum of an element from each array
- Find just strictly greater element from first array for each element in second array
- Array formed from difference of each element from the largest element in the given array
- Closest greater element for every array element from another array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

