Friday, March 12, 2021

Published March 12, 2021 by Anonymous with 0 comment

Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of connected vertices is maximum

#include <bits/stdc++.h>

using namespace std;

  

int maximumAND(int arr[], int n, int m)

{

    

    

    int tot = 1 << n;

  

    

    int mx = 0;

  

    

    for (int bm = 0; bm < tot; bm++) {

        

        

        int andans = 0;

  

        

        

        int count = 0;

  

        

        for (int i = 0; i < n; ++i) {

            

            if ((bm >> i) & 1) {

                

                if (count == 0) {

                    

                    andans = arr[i];

                }

                else {

                    

                    

                    andans = andans & arr[i];

                }

  

                

                

                count++;

            }

        }

  

        

        

        if (count == (m + 1)) {

            

            

            mx = max(mx, andans);

        }

    }

  

    

    

    return mx;

}

  

int main()

{

    int arr[] = { 1, 2, 3, 4 };

    int N = sizeof(arr) / sizeof(arr[0]);

    int M = 2;

  

    cout << maximumAND(arr, N, M);

  

    return 0;

}

Let's block ads! (Why?)

      edit

0 comments:

Post a Comment