📌  相关文章
📜  数组中每个元素与另一个数组的最大可能异或

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

数组中每个元素与另一个数组的最大可能异或

给出了由 N 个元素组成的两个数组 A 和 B。任务是计算数组 A 中每个元素与数组 B 的最大可能异或。

例子:

Input :
A : 7 3 9 12
B : 1 3 5 2
Output : 6 6 12 15
Explanation : 1 xor 7 = 6, 5 xor 3 = 6, 5
xor 9 = 12, 3 xor 12 = 15.

解决此问题的一种简单方法是检查数组 A 的每个元素与数组 B 的每个其他元素并打印最大异或。
时间复杂度:O(n^2)

一个有效的解决方案是使用 Trie 数据结构。
我们为数组 B 中所有元素的二进制表示维护一个 Trie。
现在,对于 A 的每个元素,我们在 trie 中找到最大的异或。
假设我们的数字 A[i] 是 {b1, b2…bn},其中 b1, b2…bn 是二进制位。我们从 b1 开始。
现在为了使 xor 最大,我们将在执行后尝试使最高有效位为 1
异或。因此,如果 b1 为 0,我们将需要 1,反之亦然。在 trie 中,我们转到所需的
位面。如果没有有利的选择,我们会去另一边。做这一切,
我们将获得最大的异或。

下面是实现

C++
// C++ code to find the maximum possible X-OR of
// every array element with another array
#include
using namespace std;
 
// Structure of Trie DS
struct trie
{
    int value;
    trie *child[2];
};
 
// Function to initialise a Trie Node
trie * get()
{
    trie * root = new trie;
    root -> value = 0;
    root -> child[0] = NULL;
    root -> child[1] = NULL;
    return root;
}
 
// Computing max xor
int max_xor(trie * root, int key)
{
    trie * temp = root;
     
    // Checking for all bits in integer range
    for (int i = 31; i >= 0; i--)
    {
        // Current bit in the number
        bool current_bit = ( key & ( 1 << i) );
  
        // Traversing Trie for different bit, if found
        if (temp -> child[1 - current_bit] != NULL)
            temp = temp -> child[1 - current_bit];
  
        // Traversing Trie for same bit
        else
            temp = temp -> child[current_bit];
    }
  
    // Returning xor value of maximum bit difference
    // value. Thus, we get maximum xor value
    return (key ^ temp -> value);
}
 
// Inserting B[] in Trie
void insert(trie * root, int key)
{
    trie * temp = root;
     
    // Storing 31 bits as integer representation
    for (int i = 31; i >= 0; i--)
    {
        // Current bit in the number
        bool current_bit = key & (1 << i);
         
        // New node required
        if (temp -> child[current_bit] == NULL)       
            temp -> child[current_bit] = get();
 
        // Traversing in Trie
        temp = temp -> child[current_bit];
    }
    // Assigning value to the leaf Node
    temp -> value = key;
}
 
int main()
{
    int A[] = {7, 3, 9, 12};
    int B[] = {1, 3, 5, 2};
     
    int N = sizeof(A)/sizeof(A[0]);
     
    // Forming Trie for B[]
    trie * root = get();
    for (int i = 0; i < N; i++)
        insert(root, B[i]);
     
    for (int i = 0; i < N; i++)
        cout << max_xor(root, A[i]) << endl;
     
    return 0;
}


Java
// Java code to find the maximum possible X-OR of
// every array element with another array
import java.util.*;
 
class GFG
{
 
// Structure of Trie DS
static class trie
{
    int value;
    trie []child = new trie[2];
};
 
// Function to initialise a Trie Node
static trie get()
{
    trie root = new trie();
    root.value = 0;
    root.child[0] = null;
    root.child[1] = null;
    return root;
}
 
// Computing max xor
static int max_xor(trie root, int key)
{
    trie temp = root;
     
    // Checking for all bits in integer range
    for (int i = 31; i >= 0; i--)
    {
        // Current bit in the number
        int current_bit = (key & (1 << i));
        if(current_bit > 0)
            current_bit = 1;
             
        // Traversing Trie for different bit, if found
        if (temp.child[1 - current_bit] != null)
            temp = temp.child[1 - current_bit];
 
        // Traversing Trie for same bit
        else
            temp = temp.child[current_bit];
    }
 
    // Returning xor value of maximum bit difference
    // value. Thus, we get maximum xor value
    return (key ^ temp.value);
}
 
// Inserting B[] in Trie
static void insert(trie root, int key)
{
    trie temp = root;
     
    // Storing 31 bits as integer representation
    for (int i = 31; i >= 0; i--)
    {
        // Current bit in the number
        int current_bit = key & (1 << i);
        if(current_bit > 0)
            current_bit = 1;
             
        //System.out.println(current_bit);
        // New node required
        if (temp.child[current_bit] == null)    
            temp.child[current_bit] = get();
 
        // Traversing in Trie
        temp = temp.child[current_bit];
    }
    // Assigning value to the leaf Node
    temp.value = key;
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = {7, 3, 9, 12};
    int B[] = {1, 3, 5, 2};
     
    int N = A.length;
     
    // Forming Trie for B[]
    trie root = get();
    for (int i = 0; i < N; i++)
        insert(root, B[i]);
     
    for (int i = 0; i < N; i++)
        System.out.println(max_xor(root, A[i]));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 code to find the maximum
# possible X-OR of every array
# element with another array
 
# Structure of Trie DS
class trie:
     
    def __init__(self, value: int = 0) -> None:
         
        self.value = value
        self.child = [None] * 2
 
# Function to initialise a Trie Node
def get() -> trie:
     
    root = trie()
    root.value = 0
    root.child[0] = None
    root.child[1] = None
     
    return root
 
# Computing max xor
def max_xor(root: trie, key: int) -> int:
 
    temp = root
 
    # Checking for all bits in integer range
    for i in range(31, -1, -1):
 
        # Current bit in the number
        current_bit = (key & (1 << i))
        if (current_bit > 0):
            current_bit = 1
 
        # Traversing Trie for different bit, if found
        if (temp.child[1 - current_bit] != None):
            temp = temp.child[1 - current_bit]
 
        # Traversing Trie for same bit
        else:
            temp = temp.child[current_bit]
 
    # Returning xor value of maximum bit difference
    # value. Thus, we get maximum xor value
    return (key ^ temp.value)
 
# Inserting B[] in Trie
def insert(root: trie, key: int) -> None:
 
    temp = root
 
    # Storing 31 bits as integer representation
    for i in range(31, -1, -1):
 
        # Current bit in the number
        current_bit = key & (1 << i)
        if (current_bit > 0):
            current_bit = 1
 
        # New node required
        if (temp.child[current_bit] == None):
            temp.child[current_bit] = get()
 
        # Traversing in Trie
        temp = temp.child[current_bit]
 
    # Assigning value to the leaf Node
    temp.value = key
 
# Driver Code
if __name__ == "__main__":
     
    A = [ 7, 3, 9, 12 ]
    B = [ 1, 3, 5, 2 ]
 
    N = len(A)
 
    # Forming Trie for B[]
    root = get()
    for i in range(N):
        insert(root, B[i])
 
    for i in range(N):
        print(max_xor(root, A[i]))
 
# This code is contributed by sanjeev2552


C#
// C# code to find the maximum possible X-OR of
// every array element with another array
using System;
     
class GFG
{
 
// Structure of Trie DS
class trie
{
    public int value;
    public trie []child = new trie[2];
};
 
// Function to initialise a Trie Node
static trie get()
{
    trie root = new trie();
    root.value = 0;
    root.child[0] = null;
    root.child[1] = null;
    return root;
}
 
// Computing max xor
static int max_xor(trie root, int key)
{
    trie temp = root;
     
    // Checking for all bits in integer range
    for (int i = 31; i >= 0; i--)
    {
        // Current bit in the number
        int current_bit = (key & (1 << i));
        if(current_bit > 0)
            current_bit = 1;
             
        // Traversing Trie for different bit, if found
        if (temp.child[1 - current_bit] != null)
            temp = temp.child[1 - current_bit];
 
        // Traversing Trie for same bit
        else
            temp = temp.child[current_bit];
    }
 
    // Returning xor value of maximum bit difference
    // value. Thus, we get maximum xor value
    return (key ^ temp.value);
}
 
// Inserting B[] in Trie
static void insert(trie root, int key)
{
    trie temp = root;
     
    // Storing 31 bits as integer representation
    for (int i = 31; i >= 0; i--)
    {
        // Current bit in the number
        int current_bit = key & (1 << i);
        if(current_bit > 0)
            current_bit = 1;
             
        // System.out.println(current_bit);
        // New node required
        if (temp.child[current_bit] == null)    
            temp.child[current_bit] = get();
 
        // Traversing in Trie
        temp = temp.child[current_bit];
    }
     
    // Assigning value to the leaf Node
    temp.value = key;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []A = {7, 3, 9, 12};
    int []B = {1, 3, 5, 2};
     
    int N = A.Length;
     
    // Forming Trie for B[]
    trie root = get();
    for (int i = 0; i < N; i++)
        insert(root, B[i]);
     
    for (int i = 0; i < N; i++)
        Console.WriteLine(max_xor(root, A[i]));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:

6
6
12
15

Trie形成:O(N x MAX_BITS),其中MAX_BITS是数字二进制表示的最大位数。
计算最大位差:O(N x MAX_BITS)

时间复杂度:O(N x MAX_BITS)