Problem Statement
"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
- 1) It causes more than 50% of all the values in background to be classified "low".
- 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
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
{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.
{ 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.
{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.
{921,1,5,900,8,813,3,3,3,3,3,3,3,813,813}
Returns: 4
{921,1,5,900,8,813,3,3,3,3,3,3,3,813,813,4}
Returns: 814
{1,2,3,4}
Returns: 4
{500,500,500,501,1000,503,500,500,500}
Returns: 501
{500,500,500,500,1000,503,500,500,500}
Returns: 504
{500,500,500,500,1000,503,500,500,500,4,4,4,4,4,4,4,4,4,4}
Returns: 5
{500,500,500,500,1000,503,500,500,500,4,4,4,4,4,4,4,4,4}
Returns: 504
{500,500,500,500,1000,1000,503,500,500,500,4,4,4,4,4,4,4,4,4}
Returns: 501
{30,29,28,27,26,25,26,27,28,29,27,11}
Returns: 29
{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
{18,14,19,23,3,3,3,9,19,12,2,2,1,17,13,9}
Returns: 14
{18,14,19,23,3,3,3,9,19,12,2,2,1,17,13}
Returns: 13
{1,1,1,1000,1,1,1,1}
Returns: 2
{1,1,1,1000,3,1,1,1,1}
Returns: 4
{ 1000, 1000, 1000, 1000 }
Returns: 1001
{ 3, 3, 6, 6 }
Returns: 7
{ 1000, 1000, 1000, 1000, 1000 }
Returns: 1001
{ 0, 0, 1, 1 }
Returns: 2
{ 5, 6, 12, 12, 6 }
Returns: 13
{ 10, 10, 6, 6, 6 }
Returns: 7
{ 0, 1, 2, 3 }
Returns: 3
{ 10, 13, 14, 10 }
Returns: 14
{ 1000, 1000, 1000, 1000, 1000, 1000 }
Returns: 1001
{ 500, 1000, 1000, 500 }
Returns: 1001
{ 0, 0, 0, 4, 4, 0 }
Returns: 5
{ 1000, 1000, 100, 1000, 1000 }
Returns: 1001
{ 1, 5, 6, 1, 1, 1, 1, 1, 1, 1 }
Returns: 6
{ 10, 20, 10, 20 }
Returns: 21
{ 1, 2, 3, 10, 10 }
Returns: 4
{ 4, 8, 1, 4, 9, 6, 2 }
Returns: 7
{ 2, 2, 1, 1, 1 }
Returns: 2
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 1, 1, 1, 1, 1 }
Returns: 6
{ 1, 1, 2, 2 }
Returns: 3
{ 0, 1, 0, 1 }
Returns: 2
{ 1, 2, 1, 2 }
Returns: 3
{ 1, 1, 2, 2, 1 }
Returns: 3
{ 6, 6, 6, 8, 8, 8, 6 }
Returns: 7
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 1 }
Returns: 6
{ 2, 2, 3, 3 }
Returns: 4
{ 1, 2, 3, 4 }
Returns: 4
{ 0, 0, 0, 0, 0, 10, 30, 0 }
Returns: 11
{ 8, 8, 14, 15 }
Returns: 15
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 1 }
Returns: 6
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10, 10, 1 }
Returns: 11
{ 1, 5, 3, 7 }
Returns: 6
{ 1, 4, 5, 6, 2, 3 }
Returns: 6
{ 1, 2, 12, 13 }
Returns: 13
{ 2, 2, 2, 2, 4, 7, 7, 1 }
Returns: 3
{ 2, 2, 3, 3, 2 }
Returns: 4
{ 2, 2, 2, 2, 2, 2, 2, 2, 2, 5, 5 }
Returns: 3
{ 1, 1, 1, 20, 20, 20 }
Returns: 21