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 :

  1. First, take the bitwise xor of all the element of the array and store it in variable let’s X.
  2. 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.
  3. Print the modified array.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


PHP

Output:

0 1 1 7 7

Time Complexity: O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : jit_t, Ita_c, Code_Mech