📜  使用连续元素的XOR查找数组的元素

📅  最后修改于: 2021-05-04 20:14:05             🧑  作者: Mango

给定数组arr [] ,其中原始数组的每2个连续元素的XOR值即为原始数组中元素的总数为n  那么此XOR数组的大小将为n-1。还给出了原始数组中的第一个元素。任务是找出原始数组的其余n-1个元素。

  • a,b,c,d,e,f为原始元素,并给出每2个连续元素的xor,即a ^ b = k1b ^ c = k2c ^ d = k3d ^ e = k4e ^ f = k5 (其中k1,k2,k3,k4,k5是与第一个元素a一起给出的元素),我们必须找到b,c,d,e,f的值

例子:

Input : arr[] = {13, 2, 6, 1}, a = 5
Output : 5 8 10 12 13
5^8=13, 8^10=2, 10^12=6, 12^13=1

Input : arr[] = {12, 5, 26, 7}, a = 6
Output : 6 10 15 21 18 

方法:我们可以借助a  (第一个元素),并找到下一个元素,即b  我们必须对arr [0]进行xor运算,类似地c  x或arr [1]与b等。

这通过遵循如下所述的XOR属性来起作用:

  • 数字与自身的XOR为零。
  • 给定数字本身的数字与零的XOR。

因此,因为arr [0]包含a ^ b 。所以,

a ^ arr[0] = a ^ a ^ b
           = 0 ^ b
           = b

类似地,arr [i]包含一个i和一个i + 1的XOR。所以,

ai ^ arr[i] = ai ^ ai ^ ai+1
            = 0 ^ ai+1
            = ai+1

下面是上述方法的实现

C++
// C++ program to find the array elements
// using XOR of consecutive elements
 
#include 
using namespace std;
 
// Function to find the array elements
// using XOR of consecutive elements
void getElements(int a, int arr[], int n)
{
    // array to store the orginal
    // elements
    int elements[n + 1];
 
    // first element a i.e elements[0]=a
    elements[0] = a;
 
    for (int i = 0; i < n; i++) {
 
        /*  To get the next elements we have to calculate
            xor of previous elements with given xor of 2
            consecutive elements.
            e.g. if a^b=k1 so to get b xor a both side.
            b = k1^a
        */
        elements[i + 1] = arr[i] ^ elements[i];
    }
 
    // Printing the original array elements
    for (int i = 0; i < n + 1; i++)
        cout << elements[i] << " ";
}
 
// Driver Code
int main()
{
    int arr[] = { 13, 2, 6, 1 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    int a = 5;
 
    getElements(a, arr, n);
 
    return 0;
}


Java
// Java  program to find the array elements
// using XOR of consecutive elements
 
import java.io.*;
 
class GFG {
    
 
// Function to find the array elements
// using XOR of consecutive elements
static void getElements(int a, int arr[], int n)
{
    // array to store the orginal
    // elements
    int elements[] = new int[n + 1];
 
    // first element a i.e elements[0]=a
    elements[0] = a;
 
    for (int i = 0; i < n; i++) {
 
        /* To get the next elements we have to calculate
            xor of previous elements with given xor of 2
            consecutive elements.
            e.g. if a^b=k1 so to get b xor a both side.
            b = k1^a
        */
        elements[i + 1] = arr[i] ^ elements[i];
    }
 
    // Printing the original array elements
    for (int i = 0; i < n + 1; i++)
        System.out.print( elements[i] + " ");
}
 
// Driver Code
 
    public static void main (String[] args) {
            int arr[] = { 13, 2, 6, 1 };
 
    int n = arr.length;
 
    int a = 5;
 
    getElements(a, arr, n);
    }
}
// This code is contributed by anuj_67..


Python3
# Python3 program to find the array
# elements using xor of consecutive elements
 
# Function to find the array elements
# using XOR of consecutive elements
 
def getElements(a, arr, n):
     
    # array to store the original elements
    elements = [1 for i in range(n + 1)]
     
    # first elements a i.e elements[0]=a
    elements[0] = a
     
    for i in range(n):
         
        # To get the next elements we have to
        # calculate xor of previous elements
        # with given xor of 2 consecutive elements.
        # e.g. if a^b=k1 so to get b xor a both side.
        # b = k1^a
        elements[i + 1] = arr[i] ^ elements[i]
             
    # Printing the original array elements
    for i in range(n + 1):
        print(elements[i], end = " ")
 
# Driver code
arr = [13, 2, 6, 1]
n = len(arr)
a = 5
getElements(a, arr, n)
 
# This code is contributed by Mohit Kumar


C#
// C# program to find the array elements
// using XOR of consecutive elements
 
using System;
 
class GFG {
    // Function to find the array elements
    // using XOR of consecutive elements
    static void getElements(int a, int []arr, int n)
    {
        // array to store the orginal
        // elements
        int []elements = new int[n + 1];
     
        // first element a i.e elements[0]=a
        elements[0] = a;
     
        for (int i = 0; i < n; i++) {
     
            /* To get the next elements we have to calculate
                xor of previous elements with given xor of 2
                consecutive elements.
                e.g. if a^b=k1 so to get b xor a both side.
                b = k1^a
            */
            elements[i + 1] = arr[i] ^ elements[i];
        }
     
        // Printing the original array elements
        for (int i = 0; i < n + 1; i++)
            Console.Write( elements[i] + " ");
    }
 
    // Driver Code
    public static void Main () {
            int []arr = { 13, 2, 6, 1 };
 
            int n = arr.Length;
 
            int a = 5;
 
            getElements(a, arr, n);
    }
        // This code is contributed by Ryuga
}


PHP


Javascript


输出:
5 8 10 12 13