Statistics

Problem Statement for "ClapLight"

Problem Statement

I have a light that changes from on to off or from off to on when I clap twice. The light's sensor samples the noise level in the room at short intervals and it triggers the light to change whenever it detects a low noise level followed by exactly 2 high noise levels followed by a low noise level.

"High" or "low" is based on a threshold noise level. When the sampled noise level is as high or higher than the threshold level, the noise level is classified as "high"; otherwise it is classified as "low". I have a int[] background that is a typical sequence of sensor readings when normal activity is taking place. I want software that will choose the threshold value so that it has the following properties:

  1. 1) It causes more than 50% of all the values in background to be classified "low".
  2. 2) It is the lowest possible threshold value that satisfies the 50% rule and that does not cause background to trigger the light to change.

Create a class ClapLight that contains a method threshold that is given the int[] background and that returns the desired threshold value.

Definition

Class:
ClapLight
Method:
threshold
Parameters:
int[]
Returns:
int
Method signature:
int threshold(int[] background)
(be sure your method is public)

Constraints

  • background will contain between 4 and 50 elements inclusive.
  • Each element of background will be between 0 and 1000 inclusive.

Examples

  1. {6,6,6,6,6}

    Returns: 7

    The threshold must be at least 7 to exceed more than 50% of the samples, and with the threshold set at 7 every reading will be classified "low" and the light will not be triggered.

  2. { 5,8,7,6,12,8,4,3,2,6 }

    Returns: 9

    The threshold must exceed at least 6 of these values to satisfy the 50% rule. So it must be at least 7. But with the threshold set at 7 the sequence 5, 8, 7, 6 would trigger the light. A threshold of 8 will allow the sequence 6,12,8,4 to trigger the light. A threshold of 9 will never cause this sequence to trigger the light.

  3. {8,8,8,1,1,1,1,1,1,1,1,1,1,1,2,1}

    Returns: 2

    Remember that the high noise levels must be both preceded and followed by low noise levels to trigger the light.

  4. {921,1,5,900,8,813,3,3,3,3,3,3,3,813,813}

    Returns: 4

  5. {921,1,5,900,8,813,3,3,3,3,3,3,3,813,813,4}

    Returns: 814

  6. {1,2,3,4}

    Returns: 4

  7. {500,500,500,501,1000,503,500,500,500}

    Returns: 501

  8. {500,500,500,500,1000,503,500,500,500}

    Returns: 504

  9. {500,500,500,500,1000,503,500,500,500,4,4,4,4,4,4,4,4,4,4}

    Returns: 5

  10. {500,500,500,500,1000,503,500,500,500,4,4,4,4,4,4,4,4,4}

    Returns: 504

  11. {500,500,500,500,1000,1000,503,500,500,500,4,4,4,4,4,4,4,4,4}

    Returns: 501

  12. {30,29,28,27,26,25,26,27,28,29,27,11}

    Returns: 29

  13. {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: 1001

  14. {18,14,19,23,3,3,3,9,19,12,2,2,1,17,13,9}

    Returns: 14

  15. {18,14,19,23,3,3,3,9,19,12,2,2,1,17,13}

    Returns: 13

  16. {1,1,1,1000,1,1,1,1}

    Returns: 2

  17. {1,1,1,1000,3,1,1,1,1}

    Returns: 4

  18. { 1000, 1000, 1000, 1000 }

    Returns: 1001

  19. { 3, 3, 6, 6 }

    Returns: 7

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

    Returns: 1001

  21. { 0, 0, 1, 1 }

    Returns: 2

  22. { 5, 6, 12, 12, 6 }

    Returns: 13

  23. { 10, 10, 6, 6, 6 }

    Returns: 7

  24. { 0, 1, 2, 3 }

    Returns: 3

  25. { 10, 13, 14, 10 }

    Returns: 14

  26. { 1000, 1000, 1000, 1000, 1000, 1000 }

    Returns: 1001

  27. { 500, 1000, 1000, 500 }

    Returns: 1001

  28. { 0, 0, 0, 4, 4, 0 }

    Returns: 5

  29. { 1000, 1000, 100, 1000, 1000 }

    Returns: 1001

  30. { 1, 5, 6, 1, 1, 1, 1, 1, 1, 1 }

    Returns: 6

  31. { 10, 20, 10, 20 }

    Returns: 21

  32. { 1, 2, 3, 10, 10 }

    Returns: 4

  33. { 4, 8, 1, 4, 9, 6, 2 }

    Returns: 7

  34. { 2, 2, 1, 1, 1 }

    Returns: 2

  35. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 1, 1, 1, 1, 1 }

    Returns: 6

  36. { 1, 1, 2, 2 }

    Returns: 3

  37. { 0, 1, 0, 1 }

    Returns: 2

  38. { 1, 2, 1, 2 }

    Returns: 3

  39. { 1, 1, 2, 2, 1 }

    Returns: 3

  40. { 6, 6, 6, 8, 8, 8, 6 }

    Returns: 7

  41. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 1 }

    Returns: 6

  42. { 2, 2, 3, 3 }

    Returns: 4

  43. { 1, 2, 3, 4 }

    Returns: 4

  44. { 0, 0, 0, 0, 0, 10, 30, 0 }

    Returns: 11

  45. { 8, 8, 14, 15 }

    Returns: 15

  46. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 1 }

    Returns: 6

  47. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10, 10, 1 }

    Returns: 11

  48. { 1, 5, 3, 7 }

    Returns: 6

  49. { 1, 4, 5, 6, 2, 3 }

    Returns: 6

  50. { 1, 2, 12, 13 }

    Returns: 13

  51. { 2, 2, 2, 2, 4, 7, 7, 1 }

    Returns: 3

  52. { 2, 2, 3, 3, 2 }

    Returns: 4

  53. { 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 5 }

    Returns: 3

  54. { 1, 1, 1, 20, 20, 20 }

    Returns: 21


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: