Array Partition

Sort by

recency

|

22 Discussions

|

  • + 1 comment
     from itertools import permutations as p
    import numpy as np
    def gcd(a,b): 
        # Everything divides 0  
        if (a == 0): 
            return b 
        if (b == 0):
            return a 
        # base case 
        if (a == b): 
            return a 
        # a is greater 
        if (a > b): 
            return gcd(a-b, b)    
        return gcd(a, b-a) 
    n=int(input())
    man=[]
    list5=[]
    for i in range(0,n):
        list2=[]
        w=int(input())
        list1=input().split()
        go="".join(list1)
        list2=list(p(go,w))
        print(list2)
        count=0
        final=[]
        for k in list2:
            k=list(k)
            for p in range(1,len(k)):
                e=k[0:p]
                f=k[p:]
                e=[int(i) for i in e]
                f=[int(i) for i in f]
                e=sorted(e)
                f=sorted(f)
                e=[str(i) for i in e]
                f=[str(i) for i in f]
                a1="".join(e)
                a2="".join(f)
                e=[int(i) for i in e]
                f=[int(i) for i in f]
                y=np.prod(e)
                z=np.prod(f)
                t=gcd(y,z)
                st=a1+','+a2
                if (st not in final) and (t==1):
                    final.append(st)
                    count=count+1    
                    
        print(count)           
    
  • + 0 comments

    I am not getting the problem statement. can anyone explain, please.

  • + 0 comments

    I am getting time out in the last two test cases. I have used DisjointSet and Modular Exponentiation and Euler's Algorithm to solve the problem. My code-

    static int solve(int [] a) 
        {
            DisjointSet<Integer> ds=new DisjointSet<Integer>();
            HashSet<Integer> set=new HashSet<Integer>();
            int count1=0;
            for(int i:a)
                 {
                    set.add(i);
                    ds.makeSet(i);
                    if(i==1)
                        count1++;
                    
                 }
            Iterator <Integer> i=set.iterator();
            while(i.hasNext())
            {
                int x=i.next();
                if(x==1)
                    continue;
                else
                {
                    Iterator<Integer> j=set.iterator();
                    while(j.hasNext())
                    {
                        int y=j.next();
                        if(y!=x && gcd(x,y)!=1)
                                ds.union(x,y);
                    }
                }       
            }   
            int count=ds.numberOfSets()+(count1!=0?count1-1:0);
            return (int)((power(2,count,1000000007)-2+1000000007)%1000000007);
        }
    

    Am I missing something?

  • + 1 comment

    "Note that {1, 3} will always be placed together and {1, 2} will also be placed together always. As {1} is common, therefore {1, 2, 3} will always be placed together otherwise gcd wont be 1." I don't understand this clearly ? {1,2} isn't placed together in the previous statement.

  • + 0 comments

    what means verdict: point exception?