Replace every element of the array with BitWise XOR of all other
Given an array of integers. The task is to replace every element by the bitwise xor of all other elements of the array.
Examples:
Input: arr[] = { 2, 3, 3, 5, 5 }
Output: 0 1 1 7 7
Bitwise Xor of 3, 3, 5, 5 = 0
Bitwise Xor of 2, 3, 5, 5 = 1
Bitwise Xor of 2, 3, 5, 5 = 1
Bitwise Xor of 2, 3, 3, 5 = 7
Bitwise Xor of 2, 3, 3, 5 = 7
Input : arr[] = { 1, 2, 3 }
Output : 1 2 3
Approach :
- First, take the bitwise xor of all the element of the array and store it in variable let’s X.
- Now replace each element by the xor of X and that element, since xoring the same element will cancel out leaving the xor of all the other elements.
- Print the modified array.
Below is the implementation of above approach:
C++
// C++ program to Replace every element // by the bitwise xor of all other elements #include <bits/stdc++.h> using namespace std; // Function to replace the elements void ReplaceElements(int arr[], int n) { int X = 0; // Calculate the xor of all the elements for (int i = 0; i < n; ++i) { X ^= arr[i]; } // Replace every element by the // xor of all other elements for (int i = 0; i < n; ++i) { arr[i] = X ^ arr[i]; } } // Driver code int main() { int arr[] = { 2, 3, 3, 5, 5 }; int n = sizeof(arr) / sizeof(arr[0]); ReplaceElements(arr, n); // Print the modified array. for (int i = 0; i < n; ++i) { cout << arr[i] << " "; } return 0; } |
chevron_right
filter_none
Java
// Java program to Replace every element // by the bitwise xor of all other elements import java.io.*; class GFG { // Function to replace the elements static void ReplaceElements(int arr[], int n) { int X = 0; // Calculate the xor of all the elements for (int i = 0; i < n; ++i) { X ^= arr[i]; } // Replace every element by the // xor of all other elements for (int i = 0; i < n; ++i) { arr[i] = X ^ arr[i]; } } // Driver code public static void main (String[] args) { int arr[] = { 2, 3, 3, 5, 5 }; int n = arr.length; ReplaceElements(arr, n); // Print the modified array. for (int i = 0; i < n; ++i) { System.out.print(arr[i] +" "); } } } |
chevron_right
filter_none
Python 3
# Python 3 program to Replace every element # by the bitwise xor of all other elements # Function to replace the elements def ReplaceElements(arr, n): X = 0 # Calculate the xor of all the elements for i in range(n): X ^= arr[i] # Replace every element by the # xor of all other elements for i in range(n): arr[i] = X ^ arr[i] # Driver code if __name__ == "__main__": arr = [ 2, 3, 3, 5, 5 ] n = len(arr) ReplaceElements(arr, n) # Print the modified array. for i in range(n): print(arr[i], end = " ") # This code is contributed # by ChitraNayal |
chevron_right
filter_none
C#
// C# program to Replace every element // by the bitwise xor of all other elements using System; public class GFG{ // Function to replace the elements static void ReplaceElements(int []arr, int n) { int X = 0; // Calculate the xor of all the elements for (int i = 0; i < n; ++i) { X ^= arr[i]; } // Replace every element by the // xor of all other elements for (int i = 0; i < n; ++i) { arr[i] = X ^ arr[i]; } } // Driver code static public void Main (){ int []arr = { 2, 3, 3, 5, 5 }; int n = arr.Length; ReplaceElements(arr, n); // Print the modified array. for (int i = 0; i < n; ++i) { Console.Write(arr[i] +" "); } } //This code is contributed by ajit } |
chevron_right
filter_none
PHP
Output:
0 1 1 7 7
Time Complexity: O(N)
Recommended Posts:
- Replace every array element by Bitwise Xor of previous and next element
- Replace each element by the difference of the total size of the array and frequency of that element
- Replace every element of the array by its previous element
- Replace every element of the array by sum of all other elements
- Replace every array element by sum of previous and next
- Replace every element of the array by its next element
- Replace every element of the array by product of all other elements
- Replace every array element by multiplication of previous and next
- Replace the maximum element in the array by coefficient of range
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Replace every element with the greatest element on its left side
- Replace every element with the smallest element on its left side
- Replace every element with the greatest element on right side
- Replace every element with the least greater element on its right
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.



