给定从[1, N]取值的N 个节点,一个由N 个正整数组成的数组arr[]使得第i个节点(基于 1 的索引)具有值arr[i]和一个整数M ,任务是找到由M 条边形成的无环图的节点值的最大按位异或。
例子:
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 Bitwise XOR of vertices is 1^2^3 = 0.
- {2, 3, 4}: The value of Bitwise XOR of vertices is 2^3^4 = 5.
- {1, 2, 4}: The value of Bitwise XOR of vertices is 1^2^4 = 7.
- {1, 4, 3}: The value of 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
方法:给定的问题可以通过使用具有M 条边的无环图必须具有(M + 1) 个顶点的事实来解决。因此,任务简化为找到具有(M + 1)个顶点的数组arr[]的子集的最大按位异或。请按照以下步骤解决问题:
- 初始化一个变量,比如maxAns为0 ,它存储具有M 条边的无环图的最大按位异或。
- 生成数组arr[] 的所有可能子集,并为每个子集找到子集元素的按位异或,并将maxAns的值更新为maxAns和按位异或的最大值。
- 完成以上步骤后,打印maxAns的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum Bitwise
// XOR of any subset of the array of size K
int maximumXOR(int arr[], int n, int K)
{
// Number of node must K + 1 for
// K edges
K++;
// Stores the maximum Bitwise XOR
int maxXor = INT_MIN;
// Generate all subsets of the array
for (int i = 0; i < (1 << n); i++) {
// __builtin_popcount() returns
// the number of sets bits in
// an integer
if (__builtin_popcount(i) == K) {
// Initialize current xor as 0
int cur_xor = 0;
for (int j = 0; j < n; j++) {
// If jth bit is set in i
// then include jth element
// in the current xor
if (i & (1 << j))
cur_xor = cur_xor ^ arr[j];
}
// Update the maximum Bitwise
// XOR obtained so far
maxXor = max(maxXor, cur_xor);
}
}
// Return the maximum XOR
return maxXor;
}
// Driver Code
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
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
// Function to find the maximum Bitwise
// XOR of any subset of the array of size K
static int maximumXOR(int arr[], int n, int K)
{
// Number of node must K + 1 for
// K edges
K++;
// Stores the maximum Bitwise XOR
int maxXor = Integer.MIN_VALUE;
// Generate all subsets of the array
for(int i = 0; i < (1 << n); i++)
{
// Integer.bitCount() returns
// the number of sets bits in
// an integer
if (Integer.bitCount(i) == K)
{
// Initialize current xor as 0
int cur_xor = 0;
for(int j = 0; j < n; j++)
{
// If jth bit is set in i
// then include jth element
// in the current xor
if ((i & (1 << j)) != 0)
cur_xor = cur_xor ^ arr[j];
}
// Update the maximum Bitwise
// XOR obtained so far
maxXor = Math.max(maxXor, cur_xor);
}
}
// Return the maximum XOR
return maxXor;
}
// Driver Code
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));
}
}
// This code is contributed by Kingash
Python3
# Python3 program for the above approach
# Function to find the maximum Bitwise
# XOR of any subset of the array of size K
def maximumXOR(arr, n, K):
# Number of node must K + 1 for
# K edges
K += 1
# Stores the maximum Bitwise XOR
maxXor = -10**9
# Generate all subsets of the array
for i in range(1<
C#
// C# program for the above approach
using System;
using System.Linq;
class GFG{
// Function to find the maximum Bitwise
// XOR of any subset of the array of size K
static int maximumXOR(int []arr, int n, int K)
{
// Number of node must K + 1 for
// K edges
K++;
// Stores the maximum Bitwise XOR
int maxXor = Int32.MinValue;
// Generate all subsets of the array
for(int i = 0; i < (1 << n); i++)
{
// Finding number of sets
// bits in an integer
if (Convert.ToString(i, 2).Count(c => c == '1') == K)
{
// Initialize current xor as 0
int cur_xor = 0;
for(int j = 0; j < n; j++)
{
// If jth bit is set in i
// then include jth element
// in the current xor
if ((i & (1 << j)) != 0)
cur_xor = cur_xor ^ arr[j];
}
// Update the maximum Bitwise
// XOR obtained so far
maxXor = Math.Max(maxXor, cur_xor);
}
}
// Return the maximum XOR
return maxXor;
}
// Driver code
static void Main()
{
int [] arr = { 1, 2, 3, 4 };
int N = arr.Length;
int M = 2;
Console.WriteLine(maximumXOR(arr, N, M));
}
}
// This code is contributed by jana_sayantan.
输出:
7
时间复杂度: O(N * 2 N )
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live