Skip to content Skip to sidebar Skip to footer

Grouping Of Elements Of Set With First N Natural Numbers, Excluding One Arbitrary Element, To Give Us A Sum Equal To S

Array with elements sorted in descending order - l ans=[] for t in l: if t<=S: S-=t ans.append(t) if S==0: break ans, Gives us list with selected elements. Please t

Solution 1:

No it doesn't work for all cases:

For example: S = 17 and l = [10,5,4,3]

The answer and will be: ans = [10,5] and S = 2 (because it didn't get down to 0)

But it could have been solved by: ans [10,4,3]


Solution 2:

For n=8 and omitted element=2. This won't return the right answer.


Post a Comment for "Grouping Of Elements Of Set With First N Natural Numbers, Excluding One Arbitrary Element, To Give Us A Sum Equal To S"