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
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 <bits/stdc++.h> 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[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<Integer> s = new HashSet<Integer>(); 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*/ |
Javascript
<script> // JavaScript implementation of the approach // function to count maximum // XOR value for a triplet function Maximum_xor_Triplet(n, a) { // set is used to avoid repetitions let s = new Set(); for (let i = 0; i < n; i++) { for (let j = i; j < n; j++) { // store all possible unique // XOR value of pairs s.add(a[i] ^ a[j]); } } let ans = 0; for (let i of s.values()) { for (let j = 0; j < n; j++) { // store maximum value ans = Math.max(ans, i ^ a[j]); } } document.write( ans, "<br>" ); } // Driver code let a = [ 1, 3, 8, 15 ]; let n = a.length; Maximum_xor_Triplet(n, a); </script> |
15
Complexity Analysis:
- Time Complexity: O(n*n*logn), as nested loops are used
- Auxiliary Space: O(n), as extra space of size n is used to create a set
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!