×

# Balance it (if you possibly can)

This link came up in a forum. They give you a few weights and a beam balance. The challenge is to put the weights on the balance so as to balance it. (No Pun Intended)

Go and play the game, really.

Now that you've played the game, think of a way to program your way to a solution.

More specifically, the programming problem is this:

You will be given a set of integer such that there is a subset whose sum is exactly half of the sum of the set.

Identify that subset.

2 years, 6 months ago

Sort by:

Interesting problem. Here's one solution I can come up with. I would love to see more efficient algorithms in terms of time complexity for this. The style of code is Depth First Search.

Solution in Python

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 def balanceIt(subset1, subset2 = []): ''' Initially all weights are in subset 1 Subsequently weights will be added in subset 2 removing them from subset 1 so that sum of weights in both subsets can be made equal. ''' if sum(subset2) == sum(subset1): return subset1, subset2 if sum(subset2) > sum(subset1): return None for i in subset1: subset1_new = subset1[:] subset1_new.remove(i) result = balanceIt(subset1_new, subset2+[i]) if result != None: return result # Usage: balanceIt(weights) # For instance balanceIt([1,3,2,2]) would return # ([2, 2], [1, 3]) 
· 2 years, 6 months ago