Open In App

Replace every element of the array with BitWise XOR of all other

Improve
Improve
Like Article
Like
Save
Share
Report

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 elements of the array and store it in a 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 the 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;
}


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] +" ");
         
        }
    }
}


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


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   
}


PHP




<?php
// PHP program to Replace every element
// by the bitwise xor of all other elements
 
// Function to replace the elements
function ReplaceElements($arr, $n)
{
    $X = 0;
 
    // Calculate the xor of all the elements
    for ($i = 0; $i < $n; ++$i)
    {
        $X ^= $arr[$i];
    }
 
    // Replace every element by the
    // xor of all other elements
    for ($i = 0; $i < $n; ++$i)
    {
        $arr[$i] = $X ^ $arr[$i];
    }
    return $arr;
}
 
// Driver code
$arr = array( 2, 3, 3, 5, 5 );
$n = sizeof($arr);
 
$arr1 = ReplaceElements($arr, $n);
 
// Print the modified array.
for ($i = 0; $i < $n; ++$i)
{
    echo($arr1[$i] . " ");
     
}
 
// This code is contributed
// by Mukul singh
?>


Javascript




<script>
 
// Javascript program to Replace every element
// by the bitwise xor of all other elements
 
// Function to replace the elements
function ReplaceElements(arr, n)
{
    let X = 0;
 
    // Calculate the xor of all the elements
    for (let i = 0; i < n; ++i) {
        X ^= arr[i];
    }
 
    // Replace every element by the
    // xor of all other elements
    for (let i = 0; i < n; ++i) {
        arr[i] = X ^ arr[i];
    }
}
 
// Driver code
    let arr = [ 2, 3, 3, 5, 5 ];
    let n = arr.length;
 
    ReplaceElements(arr, n);
 
    // Print the modified array.
    for (let i = 0; i < n; ++i) {
        document.write(arr[i] + " ");
    }
 
</script>


Output

0 1 1 7 7 

Time Complexity: O(N)

Auxiliary Space: O(1) because it is using constant space for variables



Last Updated : 17 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads