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
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; } |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
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*/ |
chevron_right
filter_none
Output:
15
Recommended Posts:
- Find triplets in an array whose AND is maximum
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k]
- Find number of triplets in array such that a[i]>a[j]>a[k] and i<j<k
- Triplets in array with absolute difference less than k
- Print all triplets in sorted array that form AP
- Count of triplets in an array that satisfy the given conditions
- Number of triplets in array having subarray xor equal
- Find all triplets in a sorted array that forms Geometric Progression
- Minimum cost to reach end of array array when a maximum jump of K index is allowed
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Maximum subarray sum in array formed by repeating the given array k times
- Maximum in an array that can make another array sorted
- Maximum element in an array such that its previous and next element product is maximum
- Maximum possible XOR of every element in an array with another array
- All unique triplets that sum up to a given value
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.


