📌  相关文章
📜  用所有其他的BitWise XOR替换数组中的每个元素

📅  最后修改于: 2021-04-24 15:52:36             🧑  作者: Mango

给定一个整数数组。任务是用数组中所有其他元素的按位异或替换每个元素。

例子:

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

方法 :

  1. 首先,对数组所有元素进行按位异或,并将其存储在变量X中。
  2. 现在,将每个元素替换为X和该元素的异或,因为对同一元素进行异或将抵消所有其他元素的异或。
  3. 打印修改后的数组。

下面是上述方法的实现:

C++
// C++ program to Replace every element
// by the bitwise xor of all other elements
#include 
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


输出:
0 1 1 7 7

时间复杂度:O(N)