Most Partitions such that any two numbers from two Subsets are co-prime


Given an array arr[] of size N, the duty is to search out the utmost variety of subsets that may be obtained from the given array such that the GCD of each a and b that belong to 2 totally different subsets is 1.

Instance:

Enter: arr[] = {2, 3, 4, 5, 6}
Output: 2

Rationalization:  Divide array into 2 subset corresponding to S1 = { 2, 3, 4, 6 },  
S2= { 5 }, gcd for each a and b such {that a} ∈ S1 and b ∈ S2  is 1 i.e. 
GCD(2, 5) = 1, GCD(3, 5) = 1, GCD(4, 5) = 1, GCD(6, 5) = 1. 
Therefore most variety of subsets is 2.

Enter: arr[] = { 2, 5, 13, 9, 7, 25, 21, 6 }
Output: 3

 

Strategy: 

The concept to resolve this downside is:

For every factor of the array discover all the weather which belong to similar subset. 

To do that discover parts that aren’t coprime with it. Then for these new discovered parts repeat the identical process. All these parts will belong to 1 set. In any other case, there will probably be pair from totally different subsets which aren’t coprime.

Comply with the beneath illustration for a greater understanding.

Illustration:

Think about the array arr[] = {2, 3, 4, 5, 6}

For the worth 2:
        => The non co-prime parts are {4, 6}.
        => So the group will include {2, 4, 6}.

For the values 4 and 6:
        => The non co-prime values with 4 and 6 are {2, 6} and {2, 3, 4}
        => {2, 6} are already visited.
        => The brand new worth is 3.
        => The subset now could be {2, 4, 6, 3}.

For the worth 3:
        => The non co-prime values are {6}.
        => 6 already included within the subset.

The leftover factor is 5:
        => There isn’t any non co-prime worth for this.
        => The brand new subset {5}.

Due to this fact two subsets will be fashioned {2, 4, 6, 3} and {5}.

The next steps ought to be taken to resolve this downside:

  • Firstly, create two dictionaries, one for the quantity and one other for the elements.
  • The primary dictionary dic1 is used to test if the quantity is already current within the checklist or not.
  • For every factor in arr[], discover all the weather which can be divisible by it and retailer them in a dictionary dic2
  • Now for every factor in arr[], test whether it is current in dic1
    • If sure, then take away it from dic1 and name the operate func() recursively with the brand new dic1 and dic2
    • Within the known as operate the handed array would be the parts saved in dic2.
    • The operate will probably be known as recursively till the dic1 turns into empty. At this level, we’ve discovered all of the doable combos of numbers that may be fashioned utilizing the given array. 
  • Print the rely of such combos. 

Under is the implementation of the above strategy:

Python3

  

ans = 0

  

def gcd(a, b):

    if a == 0:

        return b

    return gcd(b % a, a)

    

def func(dic1, dic2, q, p):

      

    

    

    for i in q:

        if dic1[i]:

            dic1[i] = False

              

            

            

            

            

            

            if p == 0:

                world ans

                ans += 1

            ok = dic2[i]

              

            

            

            

            

            

            func(dic1, dic2, ok, p + 1)

  

def maxSubset(arr, n):

    dic1 = {}

    dic2 = {}

    for i in vary(n):

        dic1[arr[i]] = True

    for i in vary(n):

        r = []

        for j in vary(n):

            if i != j and gcd(arr[i], arr[j]) != 1:

                r.append(arr[j])

        dic2[arr[i]] = r

    func(dic1, dic2, arr, 0)

    return ans

  

if __name__ == '__main__':

    arr = [2, 3, 4, 5, 6]

    N = len(arr)

      

    

    print(maxSubset(arr, N))

Output

2

Time Complexity: O(N2 * logM) the place M is the utmost factor of the array 
Auxiliary House: O(1)