Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of connected vertices is maximum
Given an array arr[] consisting of values of N vertices of an initially unconnected Graph and an integer M, the task is to connect some vertices of the graph with exactly M edges, forming only one connected component, such that no cycle can be formed and Bitwise AND of the connected vertices is maximum possible.
Examples:
Input: arr[] = {1, 2, 3, 4}, M = 2
Output: 0
Explanation:
Following arrangement of M edges between the given vertices are:
1->2->3 (1 & 2 & 3 = 0).
2->3->4 (2 & 3 & 4 = 0).
3->4->1 (3 & 4 & 1 = 0).
1->2->4 (1 & 2 & 4 = 0).
Therefore, the maximum Bitwise AND value among all the cases is 0.
Input: arr[] = {4, 5, 6}, M = 2
Output: 4
Explanation:
Only possible way to add M edges is 4 -> 5 -> 6 (4 & 5 & 6 = 4).
Therefore, the maximum Bitwise AND value possible is 4.
Approach: The idea to solve the given problem is to generate all possible combinations of connecting vertices using M edges and print the maximum Bitwise AND among all possible combinations.
The total number of ways for connecting N vertices is 2N and there should be (M + 1) vertices to make only one connected component. Follow the steps to solve the given problem:
- Initialize two variables, say AND and ans as 0 to store the maximum Bitwise AND, and Bitwise AND of nodes of any possible connected component respectively.
- Iterate over the range [1, 2N] using a variable, say i, and perform the following steps:
- If i has (M + 1) set bits, then find the Bitwise AND of the position of set bits and store it in the variable, say ans.
- If the value of AND exceeds ans, then update the value of AND as ans.
- After completing the above steps, print the value of AND as the resultant maximum Bitwise AND.
Below is the implementation of the above approach:
C++
#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;
}
|
Java
import java.util.*;
class GFG{
static 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 ) != 0 )
{
if (count == 0 )
{
andans = arr[i];
}
else
{
andans = andans & arr[i];
}
count++;
}
}
if (count == (m + 1 ))
{
mx = Math.max(mx, andans);
}
}
return mx;
}
public static void main(String args[])
{
int arr[] = { 1 , 2 , 3 , 4 };
int N = arr.length;
int M = 2 ;
System.out.println(maximumAND(arr, N, M));
}
}
|
Python3
def maximumAND(arr, n, m):
tot = 1 << n
mx = 0
for bm in range (tot):
andans = 0
count = 0
for i in range (n):
if ((bm >> i) & 1 ):
if (count = = 0 ):
andans = arr[i]
else :
andans = andans & arr[i]
count + = 1
if (count = = (m + 1 )):
mx = max (mx, andans)
return mx
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 , 4 ]
N = len (arr)
M = 2
print (maximumAND(arr, N, M))
|
C#
using System;
class GFG{
static 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) != 0 )
{
if (count == 0)
{
andans = arr[i];
}
else
{
andans = andans & arr[i];
}
count++;
}
}
if (count == (m + 1))
{
mx = Math.Max(mx, andans);
}
}
return mx;
}
static public void Main ()
{
int [] arr = { 1, 2, 3, 4 };
int N = arr.Length;
int M = 2;
Console.WriteLine(maximumAND(arr, N, M));
}
}
|
Javascript
<script>
function maximumAND(arr, n, m)
{
let tot = 1 << n;
let mx = 0;
for (let bm = 0; bm < tot; bm++)
{
let andans = 0;
let count = 0;
for (let i = 0; i < n; ++i)
{
if (((bm >> i) & 1) != 0)
{
if (count == 0)
{
andans = arr[i];
}
else
{
andans = andans & arr[i];
}
count++;
}
}
if (count == (m + 1))
{
mx = Math.max(mx, andans);
}
}
return mx;
}
let arr = [ 1, 2, 3, 4 ];
let N = arr.length;
let M = 2;
document.write(maximumAND(arr, N, M));
</script>
|
Time Complexity: O((2N)*N)
Auxiliary Space: O(N)
Last Updated :
30 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...