Such a strggle to solve this type of questions


#1

Even after 2 hours of effort of solving this question I came very close but can’t cross the line.Finally at last I have to google this question and I saw my program(previous one) is close to that solution (but who cares about closeness of program , program should pass all test cases)

This is final python 3 solution : )

def init(self):
self.subset=[]

def real(self,comb,target,curr,ind,A1):
    if(curr==target):
        if(comb not in self.subset):
            self.subset.append(list(comb))
    for i in range(ind,len(A1)):
        #print("index:",i,"element:",A1[i])
        
        if(curr+A1[i]>target):
            #print("pop++++++++")
            break
        comb.append(A1[i])
        #print("comb",comb)
        #print("sum-----",curr+A1[i])
        #print()
        
        self.real(comb,target,curr+A1[i],i,A1)
        #print("POP@@@@@@@@@@")
        comb.pop()
    return(self.subset)
        
def combinationSum(self, A, B):
    A.sort()
    Z=self.real([],B,0,0,A)
    Z.sort()
    return(Z)