Maximum Bitwise XOR of node values of an Acyclic Graph made up of N given vertices using M edges
Given N nodes valued by [1, N], an array arr[] consisting of N positive integers such that the ith node ( 1-based indexing ) has the value arr[i] and an integer M, the task is to find the maximum Bitwise XOR of node values of an acyclic graph formed by M edges.
Examples:
Input: arr[]= {1, 2, 3, 4}, M = 2
Output: 7
Explanation:
Acyclic graphs having M(= 2) edges can be formed by vertices as:
- {1, 2, 3}: The value of the Bitwise XOR of vertices is 1^2^3 = 0.
- {2, 3, 4}: The value of the Bitwise XOR of vertices is 2^3^4 = 5.
- {1, 2, 4}: The value of the Bitwise XOR of vertices is 1^2^4 = 7.
- {1, 4, 3}: The value of the Bitwise XOR of vertices is 1^4^3 = 6.
Therefore, the maximum Bitwise XOR among all possible acyclic graphs is 7.
Input: arr[] = {2, 4, 8, 16}, M = 2
Output: 28
Approach: The given problem can be solved by using the fact that an acyclic graph having M edges must have (M + 1) vertices. Therefore, the task is reduced to finding the maximum Bitwise XOR of a subset of the array arr[] having (M + 1) vertices. Follow the steps below to solve the problem:
- Initialize a variable, say maxAns as 0 that stores the maximum Bitwise XOR of an acyclic graph having M edges.
- Generate all possible subsets of the array arr[] and for each subset find the Bitwise XOR of the elements of the subset and update the value of maxAns to the maximum of maxAns and Bitwise XOR.
- After completing the above steps, print the value of maxAns as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximumXOR( int arr[], int n, int K)
{
K++;
int maxXor = INT_MIN;
for ( int i = 0; i < (1 << n); i++) {
if (__builtin_popcount(i) == K) {
int cur_xor = 0;
for ( int j = 0; j < n; j++) {
if (i & (1 << j))
cur_xor = cur_xor ^ arr[j];
}
maxXor = max(maxXor, cur_xor);
}
}
return maxXor;
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int N = sizeof (arr) / sizeof ( int );
int M = 2;
cout << maximumXOR(arr, N, M);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int maximumXOR( int arr[], int n, int K)
{
K++;
int maxXor = Integer.MIN_VALUE;
for ( int i = 0 ; i < ( 1 << n); i++)
{
if (Integer.bitCount(i) == K)
{
int cur_xor = 0 ;
for ( int j = 0 ; j < n; j++)
{
if ((i & ( 1 << j)) != 0 )
cur_xor = cur_xor ^ arr[j];
}
maxXor = Math.max(maxXor, cur_xor);
}
}
return maxXor;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int N = arr.length;
int M = 2 ;
System.out.println(maximumXOR(arr, N, M));
}
}
|
Python3
def maximumXOR(arr, n, K):
K + = 1
maxXor = - 10 * * 9
for i in range ( 1 <<n):
if ( bin (i).count( '1' ) = = K):
cur_xor = 0
for j in range (n):
if (i & ( 1 << j)):
cur_xor = cur_xor ^ arr[j]
maxXor = max (maxXor, cur_xor)
return maxXor
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 ]
N = len (arr)
M = 2
print (maximumXOR(arr, N, M))
|
C#
using System;
using System.Linq;
class GFG{
static int maximumXOR( int []arr, int n, int K)
{
K++;
int maxXor = Int32.MinValue;
for ( int i = 0; i < (1 << n); i++)
{
if (Convert.ToString(i, 2).Count(c => c == '1' ) == K)
{
int cur_xor = 0;
for ( int j = 0; j < n; j++)
{
if ((i & (1 << j)) != 0)
cur_xor = cur_xor ^ arr[j];
}
maxXor = Math.Max(maxXor, cur_xor);
}
}
return maxXor;
}
static void Main()
{
int [] arr = { 1, 2, 3, 4 };
int N = arr.Length;
int M = 2;
Console.WriteLine(maximumXOR(arr, N, M));
}
}
|
Javascript
<script>
function maximumXOR(arr, n, K) {
K++;
let maxXor = Number.MIN_SAFE_INTEGER;
for (let i = 0; i < (1 << n); i++) {
if ((i).toString(2).split( '' ).
filter(x => x == '1' ).length == K) {
let cur_xor = 0;
for (let j = 0; j < n; j++) {
if (i & (1 << j))
cur_xor = cur_xor ^ arr[j];
}
maxXor = Math.max(maxXor, cur_xor);
}
}
return maxXor;
}
let arr = [1, 2, 3, 4];
let N = arr.length;
let M = 2;
document.write(maximumXOR(arr, N, M));
</script>
|
Time Complexity: O(N * 2N)
Auxiliary Space: O(1)
Last Updated :
10 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...