📌  相关文章
📜  使用所有相邻元素的XOR值查找原始数组

📅  最后修改于: 2021-05-05 03:07:29             🧑  作者: Mango

给定N-1个元素的序列arr [] ,它是数组中所有相邻对的异或,任务是从arr []中找到原始数组

注意:假定N始终是奇数,并且arr []包含N个自然数的排列。

例子:

方法:

  1. 想法是找到1到N的所有元素的XOR和给定数组的相邻元素的xor,以找到期望数组的最后一个元素。
  2. 由于相邻元素的XOR将包含除最后一个元素以外的所有元素,因此,此元素与从1到N的所有数字的XOR将给出预期置换的最后一个元素。
    例如:
    Let's the expected array be - {a, b, c, d, e}
    Then the XOR array for this array will be - 
    {a^b, b^c, c^d, d^e}
    
    Now XOR of all the element from 1 to N -
    xor_all => a ^ b ^ c ^ d ^ e
    
    XOR of the adjacent elements -
    xor_adjacent => ((a ^ b) ^ (c ^ d))
    
    Now the XOR of the both the array will be the 
    last element of the expected permutation 
    => (a ^ b ^ c ^ d ^ e) ^ ((a ^ b) ^ (c ^ d))
    => As all elements are in pair except the last element.
    => (a ^ a ^ b ^ b ^ c ^ c ^ d ^ d ^ e)
    => (0 ^ 0 ^ 0 ^ 0 ^ e)
    => e
    
  3. 现在对于元素的其余部分,在最后一个元素的xor上,我们将连续获得最后一个第二个元素,即d
  4. 重复地,更新最后一个元素,最后得到第一个元素,即a

下面是上述方法的实现:

C++
// C++ implementation to find the
// Array from the XOR array
// of the adjacent elements of array
  
#include 
using namespace std;
  
// XOR of all elements from 1 to N
int xor_all_elements(int n)
{
  
    switch (n & 3) {
  
    case 0:
        return n;
    case 1:
        return 1;
    case 2:
        return n + 1;
    case 3:
        return 0;
    }
}
  
// Function to find the Array
// from the XOR Array
vector findArray(int xorr[], int n)
{
    // Take a vector to store
    // the permuatation
    vector arr;
  
    // XOR of N natural numbers
    int xor_all = xor_all_elements(n);
    int xor_adjacent = 0;
  
    // Loop to find the XOR of
    // adjacent elements of the XOR Array
    for (int i = 0; i < n - 1; i += 2) {
        xor_adjacent = xor_adjacent ^ xorr[i];
    }
    int last_element = xor_all ^ xor_adjacent;
    arr.push_back(last_element);
  
    // Loop to find the other
    // elements of the permuatation
    for (int i = n - 2; i >= 0; i--) {
        // Finding the next and next elements
        last_element = xorr[i] ^ last_element;
        arr.push_back(last_element);
    }
  
    return arr;
}
  
// Driver Code
int main()
{
    vector arr;
  
    int xorr[] = { 7, 5, 3, 7 };
    int n = 5;
  
    arr = findArray(xorr, n);
  
    // Required Permutation
    for (int i = n - 1; i >= 0; i--) {
        cout << arr[i] << " ";
    }
}


Java
// Java implementation to find the
// Array from the XOR array
// of the adjacent elements of array
import java.util.*;
  
class GFG{
  
// XOR of all elements from 1 to N
static int xor_all_elements(int n)
{
  
    switch (n & 3) {
  
    case 0:
        return n;
    case 1:
        return 1;
    case 2:
        return n + 1; 
    }
    return 0;
}
  
// Function to find the Array
// from the XOR Array
static Vector findArray(int xorr[], int n)
{
    // Take a vector to store
    // the permuatation
    Vector arr = new Vector();
  
    // XOR of N natural numbers
    int xor_all = xor_all_elements(n);
    int xor_adjacent = 0;
  
    // Loop to find the XOR of
    // adjacent elements of the XOR Array
    for (int i = 0; i < n - 1; i += 2) {
        xor_adjacent = xor_adjacent ^ xorr[i];
    }
    int last_element = xor_all ^ xor_adjacent;
    arr.add(last_element);
  
    // Loop to find the other
    // elements of the permuatation
    for (int i = n - 2; i >= 0; i--)
    {
        // Finding the next and next elements
        last_element = xorr[i] ^ last_element;
        arr.add(last_element);
    }
  
    return arr;
}
  
// Driver Code
public static void main(String[] args)
{
    Vector arr = new Vector();
  
    int xorr[] = { 7, 5, 3, 7 };
    int n = 5;
  
    arr = findArray(xorr, n);
  
    // Required Permutation
    for (int i = n - 1; i >= 0; i--) 
    {
        System.out.print(arr.get(i)+ " ");
    }
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation to find the
# Array from the XOR array
# of the adjacent elements of array
  
# XOR of all elements from 1 to N
def xor_all_elements(n):
  
    if n & 3 == 0:
        return n
    elif n & 3 == 1:
        return 1
    elif n & 3 == 2:
        return n + 1
    else:
        return 0
  
# Function to find the Array
# from the XOR Array
def findArray(xorr, n):
      
    # Take a vector to store
    # the permuatation
    arr = []
  
    # XOR of N natural numbers
    xor_all = xor_all_elements(n)
    xor_adjacent = 0
  
    # Loop to find the XOR of
    # adjacent elements of the XOR Array
    for i in range(0, n - 1, 2):
        xor_adjacent = xor_adjacent ^ xorr[i]
  
    last_element = xor_all ^ xor_adjacent
    arr.append(last_element)
  
    # Loop to find the other
    # elements of the permuatation
    for i in range(n - 2, -1, -1):
          
        # Finding the next and next elements
        last_element = xorr[i] ^ last_element
        arr.append(last_element)
  
    return arr
  
# Driver Code
xorr = [7, 5, 3, 7]
n = 5
  
arr = findArray(xorr, n)
  
# Required Permutation
for i in range(n - 1, -1, -1):
    print(arr[i], end=" ")
      
# This code is contributed by mohit kumar 29


C#
// C# implementation to find the
// Array from the XOR array
// of the adjacent elements of array
using System;
using System.Collections.Generic;
  
class GFG{
   
// XOR of all elements from 1 to N
static int xor_all_elements(int n)
{
   
    switch (n & 3) {
   
    case 0:
        return n;
    case 1:
        return 1;
    case 2:
        return n + 1; 
    }
    return 0;
}
   
// Function to find the Array
// from the XOR Array
static List findArray(int []xorr, int n)
{
    // Take a vector to store
    // the permuatation
    List arr = new List();
   
    // XOR of N natural numbers
    int xor_all = xor_all_elements(n);
    int xor_adjacent = 0;
   
    // Loop to find the XOR of
    // adjacent elements of the XOR Array
    for (int i = 0; i < n - 1; i += 2) {
        xor_adjacent = xor_adjacent ^ xorr[i];
    }
    int last_element = xor_all ^ xor_adjacent;
    arr.Add(last_element);
   
    // Loop to find the other
    // elements of the permuatation
    for (int i = n - 2; i >= 0; i--)
    {
        // Finding the next and next elements
        last_element = xorr[i] ^ last_element;
        arr.Add(last_element);
    }
   
    return arr;
}
   
// Driver Code
public static void Main(String[] args)
{
    List arr = new List();
   
    int []xorr = { 7, 5, 3, 7 };
    int n = 5;
   
    arr = findArray(xorr, n);
   
    // Required Permutation
    for (int i = n - 1; i >= 0; i--) 
    {
        Console.Write(arr[i]+ " ");
    }
}
}
  
// This code contributed by Rajput-Ji


输出:
3 4 1 2 5

性能分析:

  • 时间复杂度:在上述方法中,我们遍历整个xor数组以找到相邻元素的XOR,那么最坏情况下的复杂度将为O(N)
  • 空间复杂度:在上述方法中,有一个向量数组用于存储数字从1到N的排列,那么空间复杂度将为O(N)