# Maximum value of XOR among all triplets of an array

Given an array of integers ‘arr’, the task is to find the maximum XOR value of any triplet pair among all the possible triplet pairs.
Note: An array element can be used more than once.

Examples:

Input: arr[] = {3, 4, 5, 6}
Output: 7
The triplet with maximum XOR value is {4, 5, 6}.

Input: arr[] = {1, 3, 8, 15}
Output: 15

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Store all possible values of XOR between all possible two-element pairs from the array in a set.
• Set data structure is used to avoid the repetitions of XOR values.
• Now, XOR between every set element and array element to get the maximum value for any triplet pair.

Below is the implementation of the above approach:

## 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<``int``> 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); ` `    ``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<``int``> s = ``new` `HashSet<``int``>(); ` ` `  `        ``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*/`

Output:

```15
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.