Statistics

Problem Statement for "ResistorCombinations"

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

  1. {"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.

  2. {"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.

  3. {"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.

  4. {"10000","2000","300","40","5"}

    20000

    Returns: 12345.0

  5. {"125.10000","00270.9","000625.55","90.100000","0015.60000"}

    153

    Returns: 152.75975812465552

  6. {"999999.00","999999","999999"}

    300000

    Returns: 333333.0

  7. {"400000","600001","500001","499999"}

    500000

    Returns: 500000.249999875

  8. {"853777","702163","913915","126697"}

    534755

    Returns: 523780.12231525953

  9. {"448873","304173","121581"}

    476997

    Returns: 448873.0

  10. {"860787","57541.1","825464","491183"}

    201374

    Returns: 226805.27487635709

  11. {"899579","732269","156698","869380","960329"}

    772070

    Returns: 772178.6356008628

  12. {"436385","125676","834882","647762"}

    281266

    Returns: 278980.2654024597

  13. {"925162","265426","490406"}

    416049

    Returns: 415984.2597796304

  14. {"947193","382699","599985","809374","816433"}

    259147

    Returns: 259838.6344007456

  15. {"427504","405799"}

    985604

    Returns: 833303.0

  16. {"562242","830593"}

    184581

    Returns: 335283.26722547895

  17. {"988440","852527","704291","21417.4","936873"}

    159341

    Returns: 214044.45202550225

  18. {"479656","512303","846709","610356","405316"}

    4024.9

    Returns: 107256.55532903652

  19. {"141271","833374","828757","436540","364303"}

    6567.19

    Returns: 68866.39716958712

  20. {"695668","770926","192186"}

    2873.56

    Returns: 125977.8828271119

  21. {"296231","352222","885810","50993.1","74265.1"}

    4427.54

    Returns: 24740.50665974997

  22. {"924257","442168","896387"}

    1708.01

    Returns: 224259.31579795023

  23. {"392282","316367","820960"}

    820.11

    Returns: 144338.4964605483

  24. {"238400","874871","934010"}

    2062.12

    Returns: 156047.4095376196

  25. {"82846.6"}

    8865.71

    Returns: 82846.6

  26. {"617848","611706","10132","301036","57484.4"}

    4609.31

    Returns: 8152.060307776573

  27. {"879985","835702","591692","284099"}

    3241.79

    Returns: 132574.12499884696

  28. {"6364.91"}

    8295.56

    Returns: 6364.91

  29. {"3962.27","1243.55","1418.52","9304.83","1159.14"}

    8925.56

    Returns: 9304.83

  30. {"8854.73","2938.5","5550.6","9320.41"}

    1884.89

    Returns: 1921.2716620788162

  31. {"1339.24","5117.09","5440.09","2677.19"}

    4742.72

    Returns: 4543.138990673527

  32. {"3332.39","815.71"}

    7079.21

    Returns: 4148.1

  33. {"1858.67","2583.1","7123.47"}

    3071.9

    Returns: 2735.8546248845682

  34. {"375.105","6796.18","6140.95","9984.37","6151.79"}

    9159.3

    Returns: 9166.266348150513

  35. {"3305.4"}

    7158

    Returns: 3305.4

  36. {"1898.59","3191.86","4797.05"}

    2143.92

    Returns: 2161.471098902655

  37. {"9675.39","4535.22","4316.4","8043.41"}

    2004.06

    Returns: 1966.149805603681

  38. {"4969.87","643.061","7115.01"}

    3.602

    Returns: 527.1973536982091

  39. {"3689.36","9050.47","7136.52","5026.72"}

    6.089

    Returns: 1387.7277841854066

  40. {"3003.13","2247.66","7818.73","5293.99","2559.97"}

    7.595

    Returns: 673.2582709227519

  41. {"9845.06","8508.73","256.095","558.245"}

    0.705

    Returns: 169.0551548251301

  42. {"5222.95","8559.87","8928.35","9934.47","7520.76"}

    0.741

    Returns: 1529.251751909195

  43. {"6723.14","7531.64","2183.74","5284.68","7176.14"}

    1.149

    Returns: 936.3119648691672

  44. {"8427.21","386.607","3224.42","4255.26","6953.79"}

    9.373

    Returns: 294.61916648751964

  45. {"79.172","9813.59","4827.15"}

    5.053

    Returns: 77.28101213697846

  46. {"7161.72","8914.66","6846.02","3909.18"}

    3.077

    Returns: 1529.7901373096863

  47. {"1350.65","1792.73","1623","7482.16","7905.45"}

    8.87

    Returns: 459.8796520046084

  48. {"9.381","7.93","7.48","6.604","3.386"}

    1.359

    Returns: 1.4153272986660834

  49. {"1.954","6.079","3.702"}

    1.365

    Returns: 1.2789441301272986

  50. {"3.732","9.836","8.432","4.563"}

    3.017

    Returns: 2.9638226242347363

  51. {"3.583","4.91","8.467"}

    8.116

    Returns: 8.467

  52. {"5.528","8.071","0.107","8.256","8.52"}

    2.353

    Returns: 2.3563180409186497

  53. {"8.154","6.43","3.477","5.048"}

    6.52

    Returns: 6.594814876533858

  54. {"3.414","0.81","5.719","7.925","8.298"}

    2.131

    Returns: 2.133947426592757

  55. {"0.751","5.022","2.817","8.652","3.924"}

    4.695

    Returns: 4.7087616787929

  56. {"1.732","1.917","4.404","2.743"}

    0.395

    Returns: 0.5914897880175376

  57. { "125.10000", "00270.9", "000625.65", "90.100001", "0015.60000" }

    153.0

    Returns: 152.76456574578333

  58. { "1", "2", "3" }

    2.0

    Returns: 2.0

  59. { "10.25", "13.31", "6.777", "12.2" }

    10.5

    Returns: 10.510805181371511

  60. { "1" }

    999999.0

    Returns: 1.0

  61. { "2", "3" }

    2.0

    Returns: 2.0

  62. { "1", "2" }

    2.0

    Returns: 2.0

  63. { "3", "4", "5", "6" }

    7.0

    Returns: 7.0

  64. { "10" }

    1.0

    Returns: 10.0

  65. { "999999", "999999", "999999", "999999", "999999" }

    999999.0

    Returns: 999999.0

  66. { "200" }

    1.0

    Returns: 200.0

  67. { "1", "2" }

    1.0

    Returns: 1.0

  68. { "4", "2" }

    2.0

    Returns: 2.0


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: