📌  相关文章
📜  数组的所有三元组之间的XOR最大值

📅  最后修改于: 2021-04-22 08:10:33             🧑  作者: Mango

给定整数数组“ arr”,任务是在所有可能的三联体对中找到任何三联体对的最大XOR值。
注意:数组元素可以多次使用。

例子:

方法:

  • 将数组中所有可能的两个元素对之间的所有XOR值存储在一个集合中。
  • 设置数据结构用于避免XOR值的重复。
  • 现在,在每个set元素和数组元素之间进行XOR,以获取任何三元组对的最大值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// function to count maximum
// XOR value for a triplet
void Maximum_xor_Triplet(int n, int a[])
{
    // set is used to avoid repetitions
    set s;
  
    for (int i = 0; i < n; i++) {
        for (int j = i; j < n; j++) {
  
            // store all possible unique
            // XOR value of pairs
            s.insert(a[i] ^ a[j]);
        }
    }
  
    int ans = 0;
  
    for (auto i : s) {
        for (int j = 0; j < n; j++) {
  
            // store maximum value
            ans = max(ans, i ^ a[j]);
        }
    }
  
    cout << ans << "\n";
}
  
// Driver code
int main()
{
    int a[] = { 1, 3, 8, 15 };
    int n = sizeof(a) / sizeof(a[0]);
    Maximum_xor_Triplet(n, a);
  
    return 0;
}


Java
// Java implementation of the approach
  
import java.util.HashSet;
  
class GFG 
{
  
    // function to count maximum
    // XOR value for a triplet
    static void Maximum_xor_Triplet(int n, int a[])
    {
        // set is used to avoid repetitions
        HashSet s = new HashSet();
  
        for (int i = 0; i < n; i++)
        {
            for (int j = i; j < n; j++)
            {
  
                // store all possible unique
                // XOR value of pairs
                s.add(a[i] ^ a[j]);
            }
        }
  
        int ans = 0;
        for (Integer i : s) 
        {
            for (int j = 0; j < n; j++)
            {
  
                // store maximum value
                ans = Math.max(ans, i ^ a[j]);
            }
        }
        System.out.println(ans);
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        int a[] = {1, 3, 8, 15};
        int n = a.length;
        Maximum_xor_Triplet(n, a);
    }
} 
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach 
  
# function to count maximum 
# XOR value for a triplet 
def Maximum_xor_Triplet(n, a): 
  
    # set is used to avoid repetitions 
    s = set() 
  
    for i in range(0, n): 
        for j in range(i, n): 
  
            # store all possible unique 
            # XOR value of pairs 
            s.add(a[i] ^ a[j]) 
  
    ans = 0
    for i in s: 
        for j in range(0, n): 
  
            # store maximum value 
            ans = max(ans, i ^ a[j]) 
  
    print(ans) 
  
# Driver code 
if __name__ == "__main__":
  
    a = [1, 3, 8, 15] 
    n = len(a) 
    Maximum_xor_Triplet(n, a) 
  
# This code is contributed 
# by Rituraj Jain


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GFG 
{
  
    // function to count maximum
    // XOR value for a triplet
    static void Maximum_xor_Triplet(int n, int []a)
    {
        // set is used to avoid repetitions
        HashSet s = new HashSet();
  
        for (int i = 0; i < n; i++)
        {
            for (int j = i; j < n; j++)
            {
  
                // store all possible unique
                // XOR value of pairs
                s.Add(a[i] ^ a[j]);
            }
        }
  
        int ans = 0;
        foreach (int i in s) 
        {
            for (int j = 0; j < n; j++)
            {
  
                // store maximum value
                ans = Math.Max(ans, i ^ a[j]);
            }
        }
        Console.WriteLine(ans);
    }
  
    // Driver code
    public static void Main(String[] args) 
    {
        int []a = {1, 3, 8, 15};
        int n = a.Length;
        Maximum_xor_Triplet(n, a);
    }
}
  
/* This code has been contributed 
by PrinciRaj1992*/


输出:
15