[facebook]Number as sum of candidate numbers

Given a number, and a series of candidate numbers, print out all combinations, so that the sum of candidate numbers equals to the given number.
Here order is not important, so don’t print the duplicated combination.
e.g. target is 7, candidate is 2,3,6,7
output should be 7 and 3+2+2 (but not print 2+3+2, 2+2+3)

Comments

  1. create a list with numbers lesser than the specified number, and this should be sorted list. Now start from left of this list and move until (list item value <= (given no/2)). Create 2 pairs, 3 pairs and so on.

    for ex : here list will be {2->3->6}
    first for 2 pairs: take 2 .. sum now needed = 7 - 2 = 5
    search 5 in list if found then {2,5} was an answer
    otherwise go on. now 2 pair is not possible with 3 also.
    3 pairs. start with 2 two's and find (7 - 2*2)= 3. match found. Answer= {2,2,3}

    ReplyDelete

Post a Comment

Popular posts from this blog

Circular game survival