Problem Statement
You will be given a list of numbers, and a target average. Compute the value of one additional number to add to the list, such that the average value of the new list is equal to target.
For example, given the list of numbers { 3.0, 7.0, 2.5 } and the target average 4.5, you would have to add the number 5.5 to the list because (3.0 + 7.0 + 2.5 + 5.5) / 4 = 4.5.
Definition
- Class:
- FixTheAverage
- Method:
- add
- Parameters:
- double[], double
- Returns:
- double
- Method signature:
- double add(double[] list, double target)
- (be sure your method is public)
Notes
- Your return value must have an absolute or relative error less than 1e-9.
Constraints
- list will contain between 1 and 10 elements, inclusive.
- Each element of list will be between 0 and 100, inclusive.
- target will be between 0 and 100, inclusive.
Examples
{ 3.0, 7.0, 2.5 }4.5
Returns: 5.5
This is the example from the problem statement.{ 72, 72, 72, 72 }72
Returns: 72.0
{ 0, 0 }100
Returns: 300.0
{ 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 }0
Returns: -1000.0
{ 0.01 }0.02
Returns: 0.03