Reemplace cada elemento de la array con BitWise XOR de todos los demás

Dada una array de enteros. La tarea es reemplazar cada elemento por el xor bit a bit de todos los demás elementos de la array.
Ejemplos: 
 

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

Acercarse : 
 

  1. Primero, tome el xor bit a bit de todos los elementos de la array y guárdelo en una variable, vamos a X.
  2. Ahora reemplace cada elemento por el xor de X y ese elemento, ya que hacer XOR en el mismo elemento cancelará dejando el xor de todos los demás elementos.
  3. Imprime la array modificada.

A continuación se muestra la implementación del enfoque anterior:
 

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>
Producción: 

0 1 1 7 7

 

Complejidad de tiempo : O(N)
 

Publicación traducida automáticamente

Artículo escrito por imdhruvgupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *