MovingAvg
SRM 304 · 2006-05-27 · by dgoodman
Problem Statement
We have a sequence of data values and want to look at the moving averages of length k of those values. Specifically, we want to know how much the largest moving average exceeds the smallest moving average.
Create a class MovingAvg that contains a method difference that is given k and
Notes
- The returned value must be accurate to within a relative or absolute value of 1E-9.
Constraints
- k will be between 1 and 10, inclusive.
- data will contain between k and 50 elements, inclusive.
- Each element of data will be between 0.0 and 1000.0 inclusive.
2
{3,8,9,15}
Returns: 6.5
The moving averages are (3+8)/2, (8+9)/2, and (9+15)/2 which are 5.5, 8.5, and 12.0. So the difference between the largest and smallest is 12.0 - 5.5 = 6.5
3
{17,6.2,19,3.4}
Returns: 4.533333333333335
The moving averages are (17+6.2+19)/3 and (6.2+19+3.4)/3 which are 14.0666666... and 9.533333...
3
{6,2.5,3.5}
Returns: 0.0
There is only 1 moving average of length 3, so the smallest and biggest moving average are the same.
2
{1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,2,3,4,5,6,7}
Returns: 8.0
5
{6,2,7,3,4,9,2,7,3,4,9,4,9,2,7,3,4,9}
Returns: 1.7999999999999998
Submissions are judged against all 37 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class MovingAvg with a public method double difference(int k, vector<double> data) · 37 test cases · 2 s / 256 MB per case