📌  相关文章
📜  使用大小为4的组中的元素的不同XOR查找数组

📅  最后修改于: 2021-04-29 11:05:22             🧑  作者: Mango

给定大小为N (N为4的倍数)的XOR查询的数组q [] ,该数组描述相同大小的数组,如下所示:

当仅给出q []时,任务是查找原始数组arr []的值。
例子:

方法:从xor的性质来看, a⊕a = 0a⊕0 = a
(a⊕b)c)⊕(b⊕c⊕d)= a⊕d (As(b⊕c)⊕(b⊕c)= 0)
因此,我们将数组分为4个元素的组,对于每个组(a,b,c,d),我们将
得到以下查询的结果:

  1. a⊕b⊕c
  2. a⊕b⊕d
  3. a⊕c⊕d
  4. b⊕c⊕d

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Utility function to print the contents of the array
void printArray(int arr[], int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
}
 
// Function to find the required array
void findArray(int q[], int n)
{
    int arr[n], ans;
    for (int k = 0, j = 0; j < n / 4; j++) {
        ans = q[k] ^ q[k + 3];
        arr[k + 1] = q[k + 1] ^ ans;
        arr[k + 2] = q[k + 2] ^ ans;
        arr[k] = q[k] ^ ((arr[k + 1]) ^ (arr[k + 2]));
        arr[k + 3] = q[k + 3] ^ (arr[k + 1] ^ arr[k + 2]);
        k += 4;
    }
 
    // Print the array
    printArray(arr, n);
}
 
// Driver code
int main()
{
    int q[] = { 4, 1, 7, 0 };
    int n = sizeof(q) / sizeof(q[0]);
    findArray(q, n);
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
    // Utility function to print
    // the contents of the array
    static void printArray(int []arr, int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
     
    // Function to find the required array
    static void findArray(int []q, int n)
    {
        int ans;
        int []arr = new int[n];
        for (int k = 0, j = 0;
                        j < n / 4; j++)
        {
            ans = q[k] ^ q[k + 3];
            arr[k + 1] = q[k + 1] ^ ans;
            arr[k + 2] = q[k + 2] ^ ans;
            arr[k] = q[k] ^ ((arr[k + 1]) ^
                             (arr[k + 2]));
            arr[k + 3] = q[k + 3] ^ (arr[k + 1] ^
                                     arr[k + 2]);
            k += 4;
        }
     
        // Print the array
        printArray(arr, n);
    }
     
    // Driver code
    public static void main(String args[])
    {
        int []q = { 4, 1, 7, 0 };
        int n = q.length;
        findArray(q, n);
    }
}
 
// This code is contributed
// by Akanksha Rai


Python3
# Python 3 implementation of the approach
 
# Utility function to print the
# contents of the array
def printArray(arr, n):
    for i in range(n):
        print(arr[i], end = " ")
 
# Function to find the required array
def findArray(q, n):
    arr = [None] * n
    k = 0
    for j in range(int(n / 4)):
        ans = q[k] ^ q[k + 3]
        arr[k + 1] = q[k + 1] ^ ans
        arr[k + 2] = q[k + 2] ^ ans
        arr[k] = q[k] ^ ((arr[k + 1]) ^
                         (arr[k + 2]))
        arr[k + 3] = q[k + 3] ^ (arr[k + 1] ^
                                 arr[k + 2])
        k += 4
 
    # Print the array
    printArray(arr, n)
 
# Driver code
if __name__ == '__main__':
    q = [4, 1, 7, 0]
    n = len(q)
    findArray(q, n)
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Utility function to print
    // the contents of the array
    static void printArray(int []arr, int n)
    {
        for (int i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
    }
     
    // Function to find the required array
    static void findArray(int []q, int n)
    {
        int ans;
        int []arr = new int[n] ;
        for (int k = 0, j = 0; j < n / 4; j++)
        {
            ans = q[k] ^ q[k + 3];
            arr[k + 1] = q[k + 1] ^ ans;
            arr[k + 2] = q[k + 2] ^ ans;
            arr[k] = q[k] ^ ((arr[k + 1]) ^ (arr[k + 2]));
            arr[k + 3] = q[k + 3] ^ (arr[k + 1] ^ arr[k + 2]);
            k += 4;
        }
     
        // Print the array
        printArray(arr, n);
    }
     
    // Driver code
    public static void Main()
    {
        int []q = { 4, 1, 7, 0 };
        int n = q.Length ;
        findArray(q, n);
    }
}
 
// This code is contributed by Ryuga


PHP


输出:
2 5 3 6

时间复杂度: O(N)
辅助空间: O(N)