TopCoder problem "BalanceScale" used in SRM 358 (Division I Level Two)



Problem Statement

    You are given a int[] weight, a set of weight values. You must choose the minimum number of weight values from the set such that every element in weight is measurable on a balance scale with the chosen weight values. Weight X is measurable with a set Y of weight values if and only if you can do the following. You place one weight with value X on either the left or right side of the scale. You then place some combination of weights on the scale with values from the set Y so that the left and right sides have the same total value. You may use each value in Y zero or more times to achieve this.



For example, consider the set of weight values { 21, 9, 6 }. You can choose the set { 9, 6 }, and every element in the original set will be measurable with this set. With weight 21, you can do the following. Place the one weight of 21 on the left side of the scale. Then place one weight of value 9 on the left side, and 5 weights of value 6 on the right side, and the scale will be balanced. With weights 9 and 6, you can simply place the weight on one side and a weight of equal value on the opposite side to balance the scale.



Return the minimum number of chosen weight values.
 

Definition

    
Class:BalanceScale
Method:takeWeights
Parameters:int[]
Returns:int
Method signature:int takeWeights(int[] weight)
(be sure your method is public)
    
 

Constraints

-weight will contain between 1 and 50 elements, inclusive.
-All elements of weight will be distinct.
-Each element of weight will be between 1 and 10,000,000, inclusive.
 

Examples

0)
    
{ 5, 4, 1, 8 }
Returns: 1
With weight value 1 we can measure all the other elements.
1)
    
{ 2, 3, 8, 9 }
Returns: 2
We choose weight values 2 and 3. Measure 8 by placing the weight of 8 on the left side, and 4 weights of value 2 on the right side. Measure 9 by placing the weight of 9 on the left side, and 3 weights of value 3 on the right side.
2)
    
{ 60, 105, 490, 42 }
Returns: 4
We can choose all weight values from the set.
3)
    
{ 15, 25, 9 }
Returns: 2

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=7845

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10768&pm=7845

Writer:

icanadi

Testers:

PabloGilberto , brett1479 , Olexiy , Andrew_Lazarev

Problem categories:

Brute Force, Math