Statistics

Problem Statement for "MeasuringTemperature"

Problem Statement

You have a device that measures temperature once a minute. Sadly, the temperature sensor of your device is not 100% reliable, and sometimes it reports wildly inaccurate values.

You don't have the resources to buy a new device so you decide to fix the problem by writing software for your device that will throw away invalid measurements. We consider a measurement invalid if:

  • the value is less than -273, or
  • each value that was measured within 2 minutes before and after this one differs from this value by more than 2

In other words, when deciding whether a measurement is valid, you usually consider the previous two, and the next two measurements.

You are given a int[] measuredValues that contains temperatures measured in the last few minutes. The temperatures are given in chronological order, i.e., the i-th value is the temperature measured i minutes after the device was started. Write a method that computes the average of the valid measurements. If no measurement is valid, return -300.0.

Definition

Class:
MeasuringTemperature
Method:
averageTemperature
Parameters:
int[]
Returns:
double
Method signature:
double averageTemperature(int[] measuredValues)
(be sure your method is public)

Notes

  • The returned value must be accurate to within a relative or absolute error of 1E-9.
  • The lowest possible temperature (in degrees Celsius) is -273.15.
  • It may seem weird that only integer temperatures are used in the input. This is only done to avoid rounding errors.

Constraints

  • measuredValues contains between 2 and 50 elements, inclusive.
  • Each element of measuredValues will be between -1000 and 1000, inclusive.

Examples

  1. {9, 11, 12, 13, 15}

    Returns: 12.0

    All measurements are valid. During this period of time it was getting warmer. The average temperature is (9+11+12+13+15)/5 = 12.

  2. {0, 0, 0, 2, 997, -1, 0}

    Returns: 0.16666666666666666

    The fifth measurement is clearly invalid. The average of the valid ones is slightly positive.

  3. {0, 0, 0, 2, -4, -1, 0}

    Returns: 0.16666666666666666

    This time, the fifth measurement is only slightly off, but still we consider it to be invalid.

  4. {0, 0, 0, 2, -3, -1, 0}

    Returns: -0.2857142857142857

    All the measurements are valid.

  5. {1,2,3,100,100,1,2}

    Returns: 29.857142857142858

    Again, all these measurements are valid. (Sadly, the sensor malfunctioned twice in a row. Our approach can't deal with this situation.)

  6. {1,2,3,4,5,6,7,10}

    Returns: 4.0

    The last measurement is invalid. (The measurements made within 2 minutes from the last measurement gave results 6 and 7, and neither of these values is close enough to 10.)

  7. {-35, -34, -34, -34, -35, 72, -34, 52, -36, -35, -36, 52, -36, -35, 981, -33}

    Returns: -34.75

    It's freezing cold and the sensor is malfunctioning quite often, but luckily we can identify all the wrong measurements (72, 52, 52, and 981) as invalid.

  8. {-273, -273, -274, -273}

    Returns: -273.0

    The third measurement gives a temperature lower than -273, and thus is considered to be invalid.

  9. {10, 20, 30, 40}

    Returns: -300.0

    No valid measurements here.

  10. {0,0}

    Returns: 0.0

  11. {10,12}

    Returns: 11.0

  12. {10,13}

    Returns: -300.0

  13. {12,10}

    Returns: 11.0

  14. {13,10}

    Returns: -300.0

  15. {-10,-12}

    Returns: -11.0

  16. {-10,-13}

    Returns: -300.0

  17. {-12,-10}

    Returns: -11.0

  18. {-13,-10}

    Returns: -300.0

  19. {0,0,-500,0,-500,1}

    Returns: 0.25

  20. {1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000}

    Returns: 1000.0

  21. {1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,-1000,1000,1000}

    Returns: 1000.0

  22. {10,10,13}

    Returns: 10.0

  23. {10,13,10}

    Returns: 10.0

  24. {13,10,10}

    Returns: 10.0

  25. {10,13,16,10}

    Returns: -300.0

  26. {10,13,16,20,10}

    Returns: -300.0

  27. {10,-274,-275,-274,10}

    Returns: -300.0

  28. {-13,12,-14,11,-15,10,-16,9,-17,8,-18,7,6,-19,5,-400,-400,4,-390,-300,-270,3,-12,3,2}

    Returns: -1.0

  29. {47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47}

    Returns: 0.0

  30. {47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,-47,47,47}

    Returns: 1.88

  31. {10,-274,11,12,-275,-286,13}

    Returns: 11.0

  32. {-3,-4,-3,-5,-3,-2,-3}

    Returns: -3.2857142857142856

  33. {-3,-3,-4,-3,-3,-2,-3,-3,-4,-3,-3,-3,-4,-3,-3,-2,-3,-3,-4,-3,-3,-2,-3,-3,-4,-3,-3,-2,-3,-3,-4,-3,-3,-2,-3,-3,-4,-3,-3,-2,-3,-2,-3,-3,-4,-3,-3,-2,-1,0}

    Returns: -2.9

  34. {-274,-273}

    Returns: -273.0

  35. {-274,273}

    Returns: -300.0

  36. {10, 13, 12 }

    Returns: 11.666666666666666

  37. {1, 1, 4 }

    Returns: 1.0

  38. {-273, -273 }

    Returns: -273.0

  39. {2, 10 }

    Returns: -300.0

  40. {10, 20, 30, 40 }

    Returns: -300.0

  41. {0, 0, 0, 2, 997, -1, 0 }

    Returns: 0.16666666666666666

  42. {-274, -275, -276 }

    Returns: -300.0

  43. {-273, -273, -274, -273, -500, -700, 700 }

    Returns: -273.0

  44. {2, -2, -2, -2, -2, 1 }

    Returns: -2.0

  45. {-310, 0, 1, 1 }

    Returns: 0.6666666666666666

  46. {-273, -273, -274, -273 }

    Returns: -273.0

  47. {1, 2 }

    Returns: 1.5

  48. {0, 0 }

    Returns: 0.0

  49. {0, 100 }

    Returns: -300.0

  50. {-272, -274 }

    Returns: -272.0

  51. {-35, -34, -34, -34, -35, 72, -34, 52, -36, -35, -36, 52, -36, -35, 981, -33 }

    Returns: -34.75

  52. {1, 5, 10 }

    Returns: -300.0

  53. {10, 20, 21, 40, 41 }

    Returns: 30.5

  54. {10, 0, 9 }

    Returns: 9.5

  55. {1, 100 }

    Returns: -300.0

  56. {1, 7 }

    Returns: -300.0

  57. {1, 2, 3, 4, 5, 6, 7, 10 }

    Returns: 4.0

  58. {9, 19 }

    Returns: -300.0

  59. {0, 4, 2, 6, 1 }

    Returns: 2.6

  60. {1, 6, 1 }

    Returns: 1.0

  61. {10, 0 }

    Returns: -300.0

  62. {1, 2, 3, 100, 100, 1, 2 }

    Returns: 29.857142857142858

  63. {1, 1, 3, 1, 1 }

    Returns: 1.4

  64. {3, 1, 4, 1, 3 }

    Returns: 2.4

  65. {0, 0, 0, 2, -3, -1, 0, -273, -272 }

    Returns: -60.77777777777778

  66. {50, 10, 11, 12, 13, 14, 70 }

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