TopCoder problem "ResistorCombinations" used in SRM 171 (Division I Level Two)



Problem Statement

    You are an electrical engineer putting together a prototype circuit in the lab. You need to add in one resistor to finish things up, but there is a problem: you need a resistor with a very exact value, and resistors are generally only guaranteed to be within 5% or 10% of the value they claim to be. You have a few resistors whose values you have measured, and you want to know how closely you can approximate the resistance you need by combining those resistors in different ways.



Every resistor has a resistance, which is measured in ohms. Multiple resistors can be added together to create a new resistor with its own resistance. Resistors can be added in series or in parallel, each way resulting in a different equivalent resistance.



Consider two resistors with resistances R1 and R2. When added in series the equivalent resistance, Req, is the sum of the resistances, so Req=R1+R2. When added in parallel, the equivalent is given by Req=1/(1/R1+1/R2)=R1R2/(R1+R2). Combining more than two resistors with the same operation (parallel or series) is the same as combining two of the resistors, then combining the result with a third resistor, then combining the result of that with another resistor, etc.



As an example, consider three resistors, with resistances of 2, 3, and 5 ohms. The 2 ohm and 3 ohm resistors can be added in series to create another 5 ohm resistor. Those two 5 ohm resistors could be added in series to create a 10 ohm resistor, or in parallel to create a 2.5 ohm resistor. You could also add all three original resistors together in parallel, in that case your new resistance would be Req=1/(1/2+1/3+1/5)=30/31.



By combining resistors in different combinations, you can get many different equivalent resistances. Given a String[], resistances, which represents the resistances of all of the resistors you have, and a double, target, return a double which is the closest resistance to target that can be made with the values in resistances.
 

Definition

    
Class:ResistorCombinations
Method:closestValue
Parameters:String[], double
Returns:double
Method signature:double closestValue(String[] resistances, double target)
(be sure your method is public)
    
 

Notes

-Adding resistors in series and in parallel are not the only ways to combine resistors, but they are the only ways that should be considered for this problem.
-Your return value must have relative or absolute error of less than 1e-9.
-Every combination of resistors must be made of one or more resistors.
 

Constraints

-resistances will contain between 1 and 5 elements, inclusive.
-Each element of resistances will be a value greater than 1e-3 and less than 1000000.
-Each element of resistances will contain only digits ('0'-'9') and at most one decimal point.
-target will be a positive value less than 1000000.
-To avoid rounding errors, there will be no two possible resistances that can be made such that one is less than target and one is greater than target and both are within 1e-3 of being the closest value to target.
 

Examples

0)
    
{"2","3","5"}
2.5
Returns: 2.5
The example from above. The target value can be exactly reached by adding the 2 and 3 ohm resistors in series to make another 5 ohm resistor, and then adding that in parallel with the original 5 ohm resistor to get a 2.5 ohm resistor.
1)
    
{"2","3","5"}
1
Returns: 0.967741935483871
The closest value in this case comes from adding all the resistors in parallel: 1/R=1/2+1/3+1/5, R=30/31.
2)
    
{"10.25","13.31","6.777","12.2"}
10.5
Returns: 10.510805181371511
In this case the best value comes from adding resistors 0 and 1 in series, resistors 2 and 3 in series, and then adding both of those in parallel.
3)
    
{"10000","2000","300","40","5"}
20000
Returns: 12345.0
4)
    
{"125.10000","00270.9","000625.55","90.100000","0015.60000"}
153
Returns: 152.75975812465552

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=4660&pm=1941

Writer:

Running Wild

Testers:

lbackstrom , schveiguy

Problem categories:

Brute Force