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++
#include <bits/stdc++.h>
using namespace std;
void Maximum_xor_Triplet( int n, int a[])
{
set< int > s;
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
s.insert(a[i] ^ a[j]);
}
}
int ans = 0;
for ( auto i : s) {
for ( int j = 0; j < n; j++) {
ans = max(ans, i ^ a[j]);
}
}
cout << ans << "\n" ;
}
int main()
{
int a[] = { 1, 3, 8, 15 };
int n = sizeof (a) / sizeof (a[0]);
Maximum_xor_Triplet(n, a);
return 0;
}
|
Java
import java.util.HashSet;
class GFG
{
static void Maximum_xor_Triplet( int n, int a[])
{
HashSet<Integer> s = new HashSet<Integer>();
for ( int i = 0 ; i < n; i++)
{
for ( int j = i; j < n; j++)
{
s.add(a[i] ^ a[j]);
}
}
int ans = 0 ;
for (Integer i : s)
{
for ( int j = 0 ; j < n; j++)
{
ans = Math.max(ans, i ^ a[j]);
}
}
System.out.println(ans);
}
public static void main(String[] args)
{
int a[] = { 1 , 3 , 8 , 15 };
int n = a.length;
Maximum_xor_Triplet(n, a);
}
}
|
Python3
def Maximum_xor_Triplet(n, a):
s = set ()
for i in range ( 0 , n):
for j in range (i, n):
s.add(a[i] ^ a[j])
ans = 0
for i in s:
for j in range ( 0 , n):
ans = max (ans, i ^ a[j])
print (ans)
if __name__ = = "__main__" :
a = [ 1 , 3 , 8 , 15 ]
n = len (a)
Maximum_xor_Triplet(n, a)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void Maximum_xor_Triplet( int n, int []a)
{
HashSet< int > s = new HashSet< int >();
for ( int i = 0; i < n; i++)
{
for ( int j = i; j < n; j++)
{
s.Add(a[i] ^ a[j]);
}
}
int ans = 0;
foreach ( int i in s)
{
for ( int j = 0; j < n; j++)
{
ans = Math.Max(ans, i ^ a[j]);
}
}
Console.WriteLine(ans);
}
public static void Main(String[] args)
{
int []a = {1, 3, 8, 15};
int n = a.Length;
Maximum_xor_Triplet(n, a);
}
}
|
Javascript
<script>
function Maximum_xor_Triplet(n, a)
{
let s = new Set();
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
s.add(a[i] ^ a[j]);
}
}
let ans = 0;
for (let i of s.values()) {
for (let j = 0; j < n; j++) {
ans = Math.max(ans, i ^ a[j]);
}
}
document.write( ans, "<br>" );
}
let a = [ 1, 3, 8, 15 ];
let n = a.length;
Maximum_xor_Triplet(n, a);
</script>
|
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
Last Updated :
07 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...