Statistics

Problem Statement for "MovingAvg"

Problem Statement

When data is collected at regular intervals, trends in the data are sometimes easier to see by looking at a moving average. The moving average of length k at a particular time is the average of the k most recently collected data values. So at time t it is the average of the data reported at times t,t-1,...,t-(k-1). The moving average is not defined at time t unless we have data for the preceding k-1 times.

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 double[] data. It returns the difference between the largest and smallest moving average of length k in data.

Definition

Class:
MovingAvg
Method:
difference
Parameters:
int, double[]
Returns:
double
Method signature:
double difference(int k, double[] data)
(be sure your method is public)

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.

Examples

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

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

  4. 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. 5

    {6,2,7,3,4,9,2,7,3,4,9,4,9,2,7,3,4,9}

    Returns: 1.7999999999999998

  6. 10

    {500,500,500,500,500,500,500,500,500,500}

    Returns: 0.0

  7. 10

    {600,500,500,500,500,500,500,500,500,500}

    Returns: 0.0

  8. 10

    {500,500,500,500,500,500,500,500,500,600}

    Returns: 0.0

  9. 10

    {500,500,500,500,500,500,500,500,500,500,600}

    Returns: 10.0

  10. 1

    {3,9,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8}

    Returns: 6.0

  11. 1

    {3,9,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,2}

    Returns: 7.0

  12. 3

    {.01,.02,.03,.001,.001}

    Returns: 0.009333333333333334

  13. 5

    {0,0.0,0,0.0,0.0,0.000001}

    Returns: 2.0E-7

  14. 1

    {15.492}

    Returns: 0.0

  15. 2

    {0,1000,0,0,1000,1000,0}

    Returns: 1000.0

  16. 9

    {17.0, 6.2, 19.0, 3.4, 4.4, 5.5, 6.6, 0.0, 0.1, 17.0 }

    Returns: 0.0

  17. 1

    {2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 }

    Returns: 7.0

  18. 1

    {10.0 }

    Returns: 0.0

  19. 1

    {1.0, 3.0, 2.0 }

    Returns: 2.0

  20. 2

    {3.0, 8.0, 9.0, 15.0 }

    Returns: 6.5

  21. 1

    {6.0, 2.5, 3.5 }

    Returns: 3.5

  22. 1

    {5.0, 6.0, 7.0, 0.0, 3.0 }

    Returns: 7.0

  23. 1

    {1.0 }

    Returns: 0.0

  24. 2

    {1.0, 2.0, 5.0, 87.0, 4.0, 4.0, 4.0, 987.0, 1.0, 1.0, 1.0 }

    Returns: 494.5

  25. 3

    {1.0, 1.0, 1.0, 12.0, 12.0, 12.0, 1.0, 1.0, 1.0 }

    Returns: 11.0

  26. 1

    {1.0, 2.0, 5.0, 2.0, 1.0 }

    Returns: 4.0

  27. 9

    {17.0, 6.2, 19.0, 3.4, 4.4, 5.5, 6.6, 0.0, 0.1, 17.0 }

    Returns: 0.0

  28. 1

    {2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 }

    Returns: 7.0

  29. 1

    {10.0 }

    Returns: 0.0

  30. 1

    {1.0, 3.0, 2.0 }

    Returns: 2.0

  31. 2

    {3.0, 8.0, 9.0, 15.0 }

    Returns: 6.5

  32. 1

    {6.0, 2.5, 3.5 }

    Returns: 3.5

  33. 1

    {5.0, 6.0, 7.0, 0.0, 3.0 }

    Returns: 7.0

  34. 1

    {1.0 }

    Returns: 0.0

  35. 2

    {1.0, 2.0, 5.0, 87.0, 4.0, 4.0, 4.0, 987.0, 1.0, 1.0, 1.0 }

    Returns: 494.5

  36. 3

    {1.0, 1.0, 1.0, 12.0, 12.0, 12.0, 1.0, 1.0, 1.0 }

    Returns: 11.0

  37. 1

    {1.0, 2.0, 5.0, 2.0, 1.0 }

    Returns: 4.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: