📜  数组中两个数的最大XOR

📅  最后修改于: 2021-05-04 16:08:05             🧑  作者: Mango

给定大小为N的非负整数的Arr数组。任务是找到数组中存在的两个数字之间的最大可能异或。

范例

天真的方法:一个简单的解决方案是生成给定数组的所有对并计算对的XOR。最后,返回最大XOR值。该解决方案需要O(N^{2})时间。

下面是上述方法的实现:

C++
// C++ implementation
#include 
using namespace std;
  
// Function to return the
// maximum xor
int max_xor(int arr[], int n)
{
  
    int maxXor = 0;
  
    // Calculating xor of each pair
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            maxXor = max(maxXor,
                         arr[i] ^ arr[j]);
        }
    }
    return maxXor;
}
  
// Driver Code
int main()
{
  
    int arr[] = { 25, 10, 2, 8, 5, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << max_xor(arr, n) << endl;
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
  
    // Function to return the
    // maximum xor
    static int max_xor(int arr[], int n) 
    {
        int maxXor = 0;
  
        // Calculating xor of each pair
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++) 
            {
                maxXor = Math.max(maxXor,
                        arr[i] ^ arr[j]);
            }
        }
        return maxXor;
    }
  
    // Driver Code
    public static void main(String[] args) 
    {
        int arr[] = {25, 10, 2, 8, 5, 3};
        int n = arr.length;
  
        System.out.println(max_xor(arr, n));
    }
} 
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation
  
# Function to return the
# maximum xor
def max_xor(arr, n):
  
    maxXor = 0;
  
    # Calculating xor of each pair
    for i in range(n):
        for j in range(i + 1, n):
            maxXor = max(maxXor,\
                         arr[i] ^ arr[j]);
  
    return maxXor;
  
# Driver Code
if __name__ == '__main__':
  
    arr = [ 25, 10, 2, 8, 5, 3 ];
    n = len(arr);
  
    print(max_xor(arr, n));
  
# This code is contributed by 29AjayKumar


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
// Function to return the
// maximum xor
static int max_xor(int []arr, int n) 
{
    int maxXor = 0;
  
    // Calculating xor of each pair
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++) 
        {
            maxXor = Math.Max(maxXor,
                              arr[i] ^ arr[j]);
        }
    }
    return maxXor;
}
  
// Driver Code
public static void Main() 
{
    int []arr = {25, 10, 2, 8, 5, 3};
    int n = arr.Length;
  
    Console.WriteLine(max_xor(arr, n));
}
}
  
// This code is contributed by AnkitRai01


C++
// C++ implementation of the above approach
  
#include 
using namespace std;
  
// Function to return the
// maximum xor
int max_xor(int arr[], int n)
{
    int maxx = 0, mask = 0;
  
    set se;
  
    for (int i = 30; i >= 0; i--) {
  
        // set the i'th bit in mask
        // like 100000, 110000, 111000..
        mask |= (1 << i);
  
        for (int i = 0; i < n; ++i) {
  
            // Just keep the prefix till
            // i'th bit neglecting all
            // the bit's after i'th bit
            se.insert(arr[i] & mask);
        }
  
        int newMaxx = maxx | (1 << i);
  
        for (int prefix : se) {
  
            // find two pair in set
            // such that a^b = newMaxx
            // which is the highest
            // possible bit can be obtained
            if (se.count(newMaxx ^ prefix)) {
                maxx = newMaxx;
                break;
            }
        }
  
        // clear the set for next
        // iteration
        se.clear();
    }
  
    return maxx;
}
  
// Driver Code
int main()
{
  
    int arr[] = { 25, 10, 2, 8, 5, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << max_xor(arr, n) << endl;
  
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
class GFG
{
  
// Function to return the
// maximum xor
static int max_xor(int arr[], int n)
{
    int maxx = 0, mask = 0;
  
    HashSet se = new HashSet();
  
    for (int i = 30; i >= 0; i--) 
    {
  
        // set the i'th bit in mask
        // like 100000, 110000, 111000..
        mask |= (1 << i);
  
        for (int j = 0; j < n; ++j) 
        {
  
            // Just keep the prefix till
            // i'th bit neglecting all
            // the bit's after i'th bit
            se.add(arr[j] & mask);
        }
  
        int newMaxx = maxx | (1 << i);
  
        for (int prefix : se)
        {
  
            // find two pair in set
            // such that a^b = newMaxx
            // which is the highest
            // possible bit can be obtained
            if (se.contains(newMaxx ^ prefix))
            {
                maxx = newMaxx;
                break;
            }
        }
  
        // clear the set for next
        // iteration
        se.clear();
    }
    return maxx;
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 25, 10, 2, 8, 5, 3 };
    int n = arr.length;
  
    System.out.println(max_xor(arr, n));
}
} 
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the above approach 
  
# Function to return the 
# maximum xor 
def max_xor( arr , n):
      
    maxx = 0
    mask = 0; 
  
    se = set()
      
    for i in range(30, -1, -1):
          
        # set the i'th bit in mask 
        # like 100000, 110000, 111000..
        mask |= (1 << i)
        newMaxx = maxx | (1 << i)
      
        for i in range(n):
              
            # Just keep the prefix till 
            # i'th bit neglecting all 
            # the bit's after i'th bit 
            se.add(arr[i] & mask)
  
        for prefix in se:
              
            # find two pair in set 
            # such that a^b = newMaxx 
            # which is the highest 
            # possible bit can be obtained
            if (newMaxx ^ prefix) in se:
                maxx = newMaxx
                break
                  
        # clear the set for next 
        # iteration 
        se.clear()
    return maxx
  
# Driver Code 
arr = [ 25, 10, 2, 8, 5, 3 ]
n = len(arr)
print(max_xor(arr, n)) 
  
# This code is contributed by ANKITKUMAR34


C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
      
class GFG
{
  
// Function to return the
// maximum xor
static int max_xor(int []arr, int n)
{
    int maxx = 0, mask = 0;
  
    HashSet se = new HashSet();
  
    for (int i = 30; i >= 0; i--) 
    {
  
        // set the i'th bit in mask
        // like 100000, 110000, 111000..
        mask |= (1 << i);
  
        for (int j = 0; j < n; ++j) 
        {
  
            // Just keep the prefix till
            // i'th bit neglecting all
            // the bit's after i'th bit
            se.Add(arr[j] & mask);
        }
  
        int newMaxx = maxx | (1 << i);
  
        foreach (int prefix in se)
        {
  
            // find two pair in set
            // such that a^b = newMaxx
            // which is the highest
            // possible bit can be obtained
            if (se.Contains(newMaxx ^ prefix))
            {
                maxx = newMaxx;
                break;
            }
        }
  
        // clear the set for next
        // iteration
        se.Clear();
    }
    return maxx;
}
  
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 25, 10, 2, 8, 5, 3 };
    int n = arr.Length;
  
    Console.WriteLine(max_xor(arr, n));
}
}
  
// This code is contributed by Princi Singh


输出:
28

时间复杂度: O(N^{2}) ,其中N是数组的大小

高效方法:该方法类似于本文的任务,该任务是查找最大AND值对
因此,想法是将问题陈述从查找数组中两个数字的最大异或更改为->查找数组中两个数字,以使其中xor等于数字X。在这种情况下, X将是我们要达到的第i个位的最大数目。

为了找到XOR运算的最大值,xor的值应将每一位都设为1,即1。在32位数字中,目标是从左到右开始获得最多的1 set。

要评估每个位,该位需要一个掩码掩码定义了答案中应该出现的位,而不应该出现的位。在这里,我们将使用mask保留输入中每个数字的前缀(这意味着,将输入中的ans保留为mask,数字中剩余多少位),直到第i位,然后输入集合中可能的数字,插入数字后,我们将评估是否可以将该位位置的最大值更新为1。

下面是上述方法的实现:

C++

// C++ implementation of the above approach
  
#include 
using namespace std;
  
// Function to return the
// maximum xor
int max_xor(int arr[], int n)
{
    int maxx = 0, mask = 0;
  
    set se;
  
    for (int i = 30; i >= 0; i--) {
  
        // set the i'th bit in mask
        // like 100000, 110000, 111000..
        mask |= (1 << i);
  
        for (int i = 0; i < n; ++i) {
  
            // Just keep the prefix till
            // i'th bit neglecting all
            // the bit's after i'th bit
            se.insert(arr[i] & mask);
        }
  
        int newMaxx = maxx | (1 << i);
  
        for (int prefix : se) {
  
            // find two pair in set
            // such that a^b = newMaxx
            // which is the highest
            // possible bit can be obtained
            if (se.count(newMaxx ^ prefix)) {
                maxx = newMaxx;
                break;
            }
        }
  
        // clear the set for next
        // iteration
        se.clear();
    }
  
    return maxx;
}
  
// Driver Code
int main()
{
  
    int arr[] = { 25, 10, 2, 8, 5, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << max_xor(arr, n) << endl;
  
    return 0;
}

Java

// Java implementation of the above approach
import java.util.*;
class GFG
{
  
// Function to return the
// maximum xor
static int max_xor(int arr[], int n)
{
    int maxx = 0, mask = 0;
  
    HashSet se = new HashSet();
  
    for (int i = 30; i >= 0; i--) 
    {
  
        // set the i'th bit in mask
        // like 100000, 110000, 111000..
        mask |= (1 << i);
  
        for (int j = 0; j < n; ++j) 
        {
  
            // Just keep the prefix till
            // i'th bit neglecting all
            // the bit's after i'th bit
            se.add(arr[j] & mask);
        }
  
        int newMaxx = maxx | (1 << i);
  
        for (int prefix : se)
        {
  
            // find two pair in set
            // such that a^b = newMaxx
            // which is the highest
            // possible bit can be obtained
            if (se.contains(newMaxx ^ prefix))
            {
                maxx = newMaxx;
                break;
            }
        }
  
        // clear the set for next
        // iteration
        se.clear();
    }
    return maxx;
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 25, 10, 2, 8, 5, 3 };
    int n = arr.length;
  
    System.out.println(max_xor(arr, n));
}
} 
  
// This code is contributed by Rajput-Ji

Python3

# Python3 implementation of the above approach 
  
# Function to return the 
# maximum xor 
def max_xor( arr , n):
      
    maxx = 0
    mask = 0; 
  
    se = set()
      
    for i in range(30, -1, -1):
          
        # set the i'th bit in mask 
        # like 100000, 110000, 111000..
        mask |= (1 << i)
        newMaxx = maxx | (1 << i)
      
        for i in range(n):
              
            # Just keep the prefix till 
            # i'th bit neglecting all 
            # the bit's after i'th bit 
            se.add(arr[i] & mask)
  
        for prefix in se:
              
            # find two pair in set 
            # such that a^b = newMaxx 
            # which is the highest 
            # possible bit can be obtained
            if (newMaxx ^ prefix) in se:
                maxx = newMaxx
                break
                  
        # clear the set for next 
        # iteration 
        se.clear()
    return maxx
  
# Driver Code 
arr = [ 25, 10, 2, 8, 5, 3 ]
n = len(arr)
print(max_xor(arr, n)) 
  
# This code is contributed by ANKITKUMAR34

C#

// C# implementation of the above approach
using System;
using System.Collections.Generic;
      
class GFG
{
  
// Function to return the
// maximum xor
static int max_xor(int []arr, int n)
{
    int maxx = 0, mask = 0;
  
    HashSet se = new HashSet();
  
    for (int i = 30; i >= 0; i--) 
    {
  
        // set the i'th bit in mask
        // like 100000, 110000, 111000..
        mask |= (1 << i);
  
        for (int j = 0; j < n; ++j) 
        {
  
            // Just keep the prefix till
            // i'th bit neglecting all
            // the bit's after i'th bit
            se.Add(arr[j] & mask);
        }
  
        int newMaxx = maxx | (1 << i);
  
        foreach (int prefix in se)
        {
  
            // find two pair in set
            // such that a^b = newMaxx
            // which is the highest
            // possible bit can be obtained
            if (se.Contains(newMaxx ^ prefix))
            {
                maxx = newMaxx;
                break;
            }
        }
  
        // clear the set for next
        // iteration
        se.Clear();
    }
    return maxx;
}
  
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 25, 10, 2, 8, 5, 3 };
    int n = arr.Length;
  
    Console.WriteLine(max_xor(arr, n));
}
}
  
// This code is contributed by Princi Singh
输出:
28

时间复杂度: O(Nlog(M)) ,其中N是数组的大小, M是数组中存在的最大数目。