📌  相关文章
📜  重新排列数组以使两个数组的相同索引元素的异或相等后,对相同索引数组元素进行按位异或

📅  最后修改于: 2022-05-13 01:56:07.497000             🧑  作者: Mango

重新排列数组以使两个数组的相同索引元素的异或相等后,对相同索引数组元素进行按位异或

给定由N个正整数组成的两个数组A[]B[] ,任务是在重新排列数组B[]之后对相同索引数组元素进行按位异或,使得数组A[的相同索引元素的按位异或]变得相等。

例子:

朴素的方法:给定的问题可以通过观察到重排次数最多为N来解决,因为A[]中的任何元素只能与B[]中的N个其他整数配对。因此, XN个候选值。现在,只需将所有候选与A[]中的每个元素进行异或,并检查B[]是否可以按该顺序排列。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find all possible values
// of Bitwise XOR such after rearranging
// the array elements the Bitwise XOR
// value at corresponding indexes is same
void findPossibleValues(int A[], int B[],
                        int n)
{
 
    // Sort the array B
    sort(B, B + n);
    int C[n];
 
    // Stores all the possible values
    // of the Bitwise XOR
    set numbers;
 
    // Iterate over the range
    for (int i = 0; i < n; i++) {
 
        // Possible value of K
        int candidate = A[0] ^ B[i];
 
        // Array B for the considered
        // value of K
        for (int j = 0; j < n; j++) {
            C[j] = A[j] ^ candidate;
        }
 
        sort(C, C + n);
        bool flag = false;
 
        // Verify if the considered value
        // satisfies the condition or not
        for (int j = 0; j < n; j++)
            if (C[j] != B[j])
                flag = true;
 
        // Insert the possible Bitwise
        // XOR value
        if (!flag)
            numbers.insert(candidate);
    }
 
    // Print all the values obtained
    for (auto x : numbers) {
        cout << x << " ";
    }
}
 
// Driver Code
int main()
{
    int A[] = { 7, 8, 14 };
    int B[] = { 5, 12, 3 };
    int N = sizeof(A) / sizeof(A[0]);
    findPossibleValues(A, B, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
 
// Function to find all possible values
// of Bitwise XOR such after rearranging
// the array elements the Bitwise XOR
// value at corresponding indexes is same
static void findPossibleValues(int A[], int B[],
                        int n)
{
 
    // Sort the array B
    Arrays.sort(B);
    int []C = new int[n];
 
    // Stores all the possible values
    // of the Bitwise XOR
    HashSet numbers = new HashSet();
 
    // Iterate over the range
    for (int i = 0; i < n; i++) {
 
        // Possible value of K
        int candidate = A[0] ^ B[i];
 
        // Array B for the considered
        // value of K
        for (int j = 0; j < n; j++) {
            C[j] = A[j] ^ candidate;
        }
 
        Arrays.sort(C);
        boolean flag = false;
 
        // Verify if the considered value
        // satisfies the condition or not
        for (int j = 0; j < n; j++)
            if (C[j] != B[j])
                flag = true;
 
        // Insert the possible Bitwise
        // XOR value
        if (!flag)
            numbers.add(candidate);
    }
 
    // Print all the values obtained
    for (int x : numbers) {
        System.out.print(x+ " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 7, 8, 14 };
    int B[] = { 5, 12, 3 };
    int N = A.length;
    findPossibleValues(A, B, N);
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python program for the above approach
 
# Function to find all possible values
# of Bitwise XOR such after rearranging
# the array elements the Bitwise XOR
# value at corresponding indexes is same
def findPossibleValues(A, B, n):
   
  # Sort the array B
  B.sort();
  C = [0] * n;
 
  # Stores all the possible values
  # of the Bitwise XOR
  numbers = set();
 
  # Iterate over the range
  for i in range(n):
     
    # Possible value of K
    candidate = A[0] ^ B[i];
 
    # Array B for the considered
    # value of K
    for j in range(n):
      C[j] = A[j] ^ candidate;
     
 
    C.sort();
    flag = False;
 
    # Verify if the considered value
    # satisfies the condition or not
    for j in range(n):
        if (C[j] != B[j]):
             flag = True;
 
    # Insert the possible Bitwise
    # XOR value
    if (not flag):
        numbers.add(candidate);
   
 
  # Print all the values obtained
  for x in numbers:
    print(x, end = " ");
   
# Driver Code
A = [7, 8, 14];
B = [5, 12, 3];
N = len(A);
findPossibleValues(A, B, N);
 
# This code is contributed by gfgking.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
// Function to find all possible values
// of Bitwise XOR such after rearranging
// the array elements the Bitwise XOR
// value at corresponding indexes is same
static void findPossibleValues(int []A, int []B,
                        int n)
{
 
    // Sort the array B
    Array.Sort(B);
    int []C = new int[n];
 
    // Stores all the possible values
    // of the Bitwise XOR
    HashSet numbers = new HashSet();
 
    // Iterate over the range
    for (int i = 0; i < n; i++) {
 
        // Possible value of K
        int candidate = A[0] ^ B[i];
 
        // Array B for the considered
        // value of K
        for (int j = 0; j < n; j++) {
            C[j] = A[j] ^ candidate;
        }
 
        Array.Sort(C);
        bool flag = false;
 
        // Verify if the considered value
        // satisfies the condition or not
        for (int j = 0; j < n; j++)
            if (C[j] != B[j])
                flag = true;
 
        // Insert the possible Bitwise
        // XOR value
        if (!flag)
            numbers.Add(candidate);
    }
 
    // Print all the values obtained
    foreach (int x in numbers) {
        Console.Write(x+ " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int []A = { 7, 8, 14 };
    int []B = { 5, 12, 3 };
    int N = A.Length;
    findPossibleValues(A, B, N);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find all possible values
// of Bitwise XOR such after rearranging
// the array elements the Bitwise XOR
// value at corresponding indexes is same
void findPossibleValues(int A[], int B[],
                        int n)
{
    // Stores the XOR of the array B[]
    int x = 0;
 
    for (int i = 0; i < n; i++) {
        x = x ^ B[i];
    }
 
    // Stores all possible value of
    // Bitwise XOR
    set numbers;
 
    // Iterate over the range
    for (int i = 0; i < n; i++) {
 
        // Possible value of K
        int candidate = A[0] ^ B[i];
        int curr_xor = x;
 
        // Array B for the considered
        // value of K
        for (int j = 0; j < n; j++) {
            int y = A[j] ^ candidate;
            curr_xor = curr_xor ^ y;
        }
 
        // This means all the elements
        // are equal
        if (curr_xor == 0)
            numbers.insert(candidate);
    }
 
    // Print all the possible value
    for (auto x : numbers) {
        cout << x << " ";
    }
}
 
// Driver Code
int main()
{
    int A[] = { 7, 8, 14 };
    int B[] = { 5, 12, 3 };
    int N = sizeof(A) / sizeof(A[0]);
    findPossibleValues(A, B, N);
 
    return 0;
}


Java
// Java code for above approach
import java.util.*;
 
class GFG{
 
// Function to find all possible values
// of Bitwise XOR such after rearranging
// the array elements the Bitwise XOR
// value at corresponding indexes is same
static void findPossibleValues(int A[], int B[],
                        int n)
{
    // Stores the XOR of the array B[]
    int x = 0;
 
    for (int i = 0; i < n; i++) {
        x = x ^ B[i];
    }
 
    // Stores all possible value of
    // Bitwise XOR
    HashSet numbers = new HashSet();
 
    // Iterate over the range
    for (int i = 0; i < n; i++) {
 
        // Possible value of K
        int candidate = A[0] ^ B[i];
        int curr_xor = x;
 
        // Array B for the considered
        // value of K
        for (int j = 0; j < n; j++) {
            int y = A[j] ^ candidate;
            curr_xor = curr_xor ^ y;
        }
 
        // This means all the elements
        // are equal
        if (curr_xor == 0)
            numbers.add(candidate);
    }
 
    // Print all the possible value
    for (int i : numbers) {
        System.out.print(i + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 7, 8, 14 };
    int B[] = { 5, 12, 3 };
    int N = A.length;
    findPossibleValues(A, B, N);
}
}
 
// This code is contributed by avijitmondal1998.


Python3
# Python 3 program for the above approach
 
# Function to find all possible values
# of Bitwise XOR such after rearranging
# the array elements the Bitwise XOR
# value at corresponding indexes is same
def findPossibleValues(A, B, n):
   
    # Stores the XOR of the array B[]
    x = 0
 
    for i in range(n):
        x = x ^ B[i]
 
    # Stores all possible value of
    # Bitwise XOR
    numbers = set()
 
    # Iterate over the range
    for i in range(n):
        # Possible value of K
        candidate = A[0] ^ B[i]
        curr_xor = x
 
        # Array B for the considered
        # value of K
        for j in range(n):
            y = A[j] ^ candidate
            curr_xor = curr_xor ^ y
 
        # This means all the elements
        # are equal
        if (curr_xor == 0):
            numbers.add(candidate)
 
    # Print all the possible value
    for x in numbers:
        print(x, end = " ")
 
# Driver Code
if __name__ == '__main__':
    A = [7, 8, 14]
    B = [5, 12, 3]
    N = len(A)
    findPossibleValues(A, B, N)
     
    # This code is contributed by ipg2016107.


C#
// C# code for above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
// Function to find all possible values
// of Bitwise XOR such after rearranging
// the array elements the Bitwise XOR
// value at corresponding indexes is same
static void findPossibleValues(int []A, int []B,
                        int n)
{
   
    // Stores the XOR of the array []B
    int x = 0;
 
    for (int i = 0; i < n; i++) {
        x = x ^ B[i];
    }
 
    // Stores all possible value of
    // Bitwise XOR
    HashSet numbers = new HashSet();
 
    // Iterate over the range
    for (int i = 0; i < n; i++) {
 
        // Possible value of K
        int candidate = A[0] ^ B[i];
        int curr_xor = x;
 
        // Array B for the considered
        // value of K
        for (int j = 0; j < n; j++) {
            int y = A[j] ^ candidate;
            curr_xor = curr_xor ^ y;
        }
 
        // This means all the elements
        // are equal
        if (curr_xor == 0)
            numbers.Add(candidate);
    }
 
    // Print all the possible value
    foreach (int i in numbers) {
        Console.Write(i + " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int []A = { 7, 8, 14 };
    int []B = { 5, 12, 3 };
    int N = A.Length;
    findPossibleValues(A, B, N);
}
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
11

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

高效方法:上述方法也可以通过不对数组进行排序并存储B[]的所有元素的按位异或,然后与C[]中的所有元素进行按位异或来进行优化。现在,如果结果为0 ,则表示两个数组具有相同的元素。请按照以下步骤解决问题:

  • 初始化变量,比如存储数组B[]的所有元素的XORx
  • 初始化一个集合,比如numbers[]以仅存储唯一数字..
  • 使用变量i迭代范围[0, N)并执行以下步骤:
    • 候选变量初始化为A[0]B[i]的 XOR 和curr_xorx执行所需操作后查看是否为0
    • 使用变量j迭代范围[0, N)并执行以下步骤:
      • 将变量y初始化为A[j]候选者的 XOR 和curr_xor 的XOR y
    • 如果curr_xor等于0,则将候选值插入集合numbers[]。
  • 执行上述步骤后,打印设置的numbers[]作为答案。

下面是上述方法的实现。

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find all possible values
// of Bitwise XOR such after rearranging
// the array elements the Bitwise XOR
// value at corresponding indexes is same
void findPossibleValues(int A[], int B[],
                        int n)
{
    // Stores the XOR of the array B[]
    int x = 0;
 
    for (int i = 0; i < n; i++) {
        x = x ^ B[i];
    }
 
    // Stores all possible value of
    // Bitwise XOR
    set numbers;
 
    // Iterate over the range
    for (int i = 0; i < n; i++) {
 
        // Possible value of K
        int candidate = A[0] ^ B[i];
        int curr_xor = x;
 
        // Array B for the considered
        // value of K
        for (int j = 0; j < n; j++) {
            int y = A[j] ^ candidate;
            curr_xor = curr_xor ^ y;
        }
 
        // This means all the elements
        // are equal
        if (curr_xor == 0)
            numbers.insert(candidate);
    }
 
    // Print all the possible value
    for (auto x : numbers) {
        cout << x << " ";
    }
}
 
// Driver Code
int main()
{
    int A[] = { 7, 8, 14 };
    int B[] = { 5, 12, 3 };
    int N = sizeof(A) / sizeof(A[0]);
    findPossibleValues(A, B, N);
 
    return 0;
}

Java

// Java code for above approach
import java.util.*;
 
class GFG{
 
// Function to find all possible values
// of Bitwise XOR such after rearranging
// the array elements the Bitwise XOR
// value at corresponding indexes is same
static void findPossibleValues(int A[], int B[],
                        int n)
{
    // Stores the XOR of the array B[]
    int x = 0;
 
    for (int i = 0; i < n; i++) {
        x = x ^ B[i];
    }
 
    // Stores all possible value of
    // Bitwise XOR
    HashSet numbers = new HashSet();
 
    // Iterate over the range
    for (int i = 0; i < n; i++) {
 
        // Possible value of K
        int candidate = A[0] ^ B[i];
        int curr_xor = x;
 
        // Array B for the considered
        // value of K
        for (int j = 0; j < n; j++) {
            int y = A[j] ^ candidate;
            curr_xor = curr_xor ^ y;
        }
 
        // This means all the elements
        // are equal
        if (curr_xor == 0)
            numbers.add(candidate);
    }
 
    // Print all the possible value
    for (int i : numbers) {
        System.out.print(i + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 7, 8, 14 };
    int B[] = { 5, 12, 3 };
    int N = A.length;
    findPossibleValues(A, B, N);
}
}
 
// This code is contributed by avijitmondal1998.

Python3

# Python 3 program for the above approach
 
# Function to find all possible values
# of Bitwise XOR such after rearranging
# the array elements the Bitwise XOR
# value at corresponding indexes is same
def findPossibleValues(A, B, n):
   
    # Stores the XOR of the array B[]
    x = 0
 
    for i in range(n):
        x = x ^ B[i]
 
    # Stores all possible value of
    # Bitwise XOR
    numbers = set()
 
    # Iterate over the range
    for i in range(n):
        # Possible value of K
        candidate = A[0] ^ B[i]
        curr_xor = x
 
        # Array B for the considered
        # value of K
        for j in range(n):
            y = A[j] ^ candidate
            curr_xor = curr_xor ^ y
 
        # This means all the elements
        # are equal
        if (curr_xor == 0):
            numbers.add(candidate)
 
    # Print all the possible value
    for x in numbers:
        print(x, end = " ")
 
# Driver Code
if __name__ == '__main__':
    A = [7, 8, 14]
    B = [5, 12, 3]
    N = len(A)
    findPossibleValues(A, B, N)
     
    # This code is contributed by ipg2016107.

C#

// C# code for above approach
using System;
using System.Collections.Generic;
 
public class GFG
{
 
// Function to find all possible values
// of Bitwise XOR such after rearranging
// the array elements the Bitwise XOR
// value at corresponding indexes is same
static void findPossibleValues(int []A, int []B,
                        int n)
{
   
    // Stores the XOR of the array []B
    int x = 0;
 
    for (int i = 0; i < n; i++) {
        x = x ^ B[i];
    }
 
    // Stores all possible value of
    // Bitwise XOR
    HashSet numbers = new HashSet();
 
    // Iterate over the range
    for (int i = 0; i < n; i++) {
 
        // Possible value of K
        int candidate = A[0] ^ B[i];
        int curr_xor = x;
 
        // Array B for the considered
        // value of K
        for (int j = 0; j < n; j++) {
            int y = A[j] ^ candidate;
            curr_xor = curr_xor ^ y;
        }
 
        // This means all the elements
        // are equal
        if (curr_xor == 0)
            numbers.Add(candidate);
    }
 
    // Print all the possible value
    foreach (int i in numbers) {
        Console.Write(i + " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int []A = { 7, 8, 14 };
    int []B = { 5, 12, 3 };
    int N = A.Length;
    findPossibleValues(A, B, N);
}
}
 
// This code is contributed by shikhasingrajput

Javascript


输出:
11

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