📌  相关文章
📜  根据数组的所有元素的异或构造一个数组,但相同索引处的元素除外

📅  最后修改于: 2021-05-25 10:00:44             🧑  作者: Mango

给定具有n个正元素的数组A []。创建另一个数组B []如B [i]的任务是对数组A []除A [i]之外的所有元素进行XOR。
例子 :

Input : A[] = {2, 1, 5, 9}
Output : B[] = {13, 14, 10, 6}

Input : A[] = {2, 1, 3, 6}
Output : B[] = {4, 7, 5, 0}

天真的方法:
我们可以简单地将B [i]计算为A []除A [i]以外的所有元素的XOR,如下

for (int i = 0; i < n; i++)
{
    B[i] = 0;
    for (int j = 0; j < n; j++)
        if ( i != j)
            B[i] ^= A[j];
}

这种幼稚方法的时间复杂度为O(n ^ 2)。
这种幼稚方法的辅助空间为O(n)。
优化方法:
首先计算数组A []的所有元素的XOR,例如’xor’,然后为数组A []的每个元素计算A [i] = xor ^ A [i]

int xor = 0;
for (int i = 0; i < n; i++)
    xor ^= A[i];

for (int i = 0; i < n; i++)
        A[i] = xor ^ A[i];

此方法的时间复杂度为O(n)。
此方法的辅助空间为O(1)。

C++
// C++ program to construct array from
// XOR of elements of given array
#include 
using namespace std;
 
// function to construct new array
void constructXOR(int A[], int n)
{
    // calculate xor of array
    int XOR = 0;
    for (int i = 0; i < n; i++)
        XOR ^= A[i];
 
    // update array
    for (int i = 0; i < n; i++)
        A[i] = XOR ^ A[i];
}
 
// Driver code
int main()
{
    int A[] = { 2, 4, 1, 3, 5};
    int n = sizeof(A) / sizeof(A[0]);
    constructXOR(A, n);
 
    // print result
    for (int i = 0; i < n; i++)
        cout << A[i] << " ";
    return 0;
}


Java
// Java program to construct array from
// XOR of elements of given array
class GFG
{
     
    // function to construct new array
    static void constructXOR(int A[], int n)
    {
         
        // calculate xor of array
        int XOR = 0;
        for (int i = 0; i < n; i++)
            XOR ^= A[i];
     
        // update array
        for (int i = 0; i < n; i++)
            A[i] = XOR ^ A[i];
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int A[] = { 2, 4, 1, 3, 5};
        int n = A.length;
        constructXOR(A, n);
     
        // print result
        for (int i = 0; i < n; i++)
            System.out.print(A[i] + " ");
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python 3 program to construct
# array from XOR of elements
# of given array
 
# function to construct new array
def constructXOR(A, n):
     
    # calculate xor of array
    XOR = 0
    for i in range(0, n):
        XOR ^= A[i]
 
    # update array
    for i in range(0, n):
        A[i] = XOR ^ A[i]
 
# Driver code
A = [ 2, 4, 1, 3, 5 ]
n = len(A)
constructXOR(A, n)
 
# print result
for i in range(0,n):
    print(A[i], end =" ")
 
# This code is contributed by Smitha Dinesh Semwal


C#
// C# program to construct array from
// XOR of elements of given array
using System;
 
class GFG
{
     
    // function to construct new array
    static void constructXOR(int []A, int n)
    {
         
        // calculate xor of array
        int XOR = 0;
        for (int i = 0; i < n; i++)
            XOR ^= A[i];
     
        // update array
        for (int i = 0; i < n; i++)
            A[i] = XOR ^ A[i];
    }
     
    // Driver code
    public static void Main()
    {
        int []A = { 2, 4, 1, 3, 5};
        int n = A.Length;
        constructXOR(A, n);
     
        // print result
        for (int i = 0; i < n; i++)
        Console.Write(A[i] + " ");
    }
}
 
// This code is contributed by nitin mittal


PHP


Javascript


输出:

3 5 0 2 4