Problem Statement
You are building an experimental car transmission by connecting successive cogwheels. Your boss wants you to assemble the wheels for the greatest possible speed. For this task, you must assemble a succession of wheels, each one driving the next one, so that the ratio between the first and last wheels is the maximum possible. Your boss allows you use as many wheels as you need, with only one constraint: the ratio of rotation between adjacent wheels cannot be greater than 2.0 (he thinks wheel teeth break easily if differences in wheel sizes are greater than this number).
Take for example, the following wheel set, where the annotation in the wheels indicate their number of teeth:
The int[] wheels specifies the number of teeth in each of the available wheels. You are to return the maximum ratio of speed between first and last wheels. If no transmission can be assembled, return 1.0.
Definition
- Class:
- CogWheel
- Method:
- maximumSpeed
- Parameters:
- int[]
- Returns:
- double
- Method signature:
- double maximumSpeed(int[] wheels)
- (be sure your method is public)
Notes
- The returned value must be accurate to within a relative or absolute value of 1E-9.
Constraints
- wheels will contain between 1 and 20 elements, inclusive.
- All elements in wheels will be between 1 and 100, inclusive.
Examples
{12,18,16,20}Returns: 1.6666666666666667
The example from the statement.{10,20}Returns: 2.0
Using the 20-teeth wheel to move the 10-wheel makes the second wheel rotate twice as fast as the first wheel.{21,12,8}Returns: 2.625
All wheels in succession can give the amazing rotation ratio of 2.625.{1,2,4,10,20,40,80}Returns: 8.0