📌  相关文章
📜  由M个边组成的N个给定顶点组成的非循环图的节点值的最大按位XOR

📅  最后修改于: 2021-05-25 04:17:28             🧑  作者: Mango

给定N个节点,它们的值分别为[1,N] ,则数组arr []N个正整数组成,因此第i节点(基于1的索引)的值为arr [i]和整数M ,任务是查找由M条边形成的非循环图的节点值的最大按位XOR。

例子:

方法:可以通过使用具有M个边的非循环图必须具有(M +1)个顶点这一事实来解决给定的问题。因此,任务减少为找到具有(M +1)个顶点的数组arr []的子集的最大按位XOR。请按照以下步骤解决问题:

  • 初始化一个变量,例如maxAns0 ,该变量存储具有M个边的无环图的最大按位XOR。
  • 生成数组arr []的所有可能子集,并为每个子集找到子集元素的按位XOR,并将maxAns的值更新为maxAnsBitwise XOR的最大值。
  • 完成上述步骤后,输出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<


输出:
7

时间复杂度: O(N * 2 N )
辅助空间: O(1)