Problem Statement
Create a method named probability that accepts a int[] weldLocations and int L as parameters. It should calculate the probability of one or more resulting pipes being strictly longer than L if the two cut locations are chosen at random from weldLocations. Each element in weldLocations represents the number of meters from the left end of the pipe.
Definition
- Class:
- PipeCuts
- Method:
- probability
- Parameters:
- int[], int
- Returns:
- double
- Method signature:
- double probability(int[] weldLocations, int L)
- (be sure your method is public)
Notes
- Your return value must have a relative or absolute error less than 1e-9.
Constraints
- weldLocations will have between 2 and 50 elements, inclusive.
- Each element in weldLocations will be between 1 and 99, inclusive.
- weldLocations will not contain duplicate elements.
- L will be between 1 and 100, inclusive.
Examples
{25, 50, 75}
25
Returns: 1.0
Any random set of cuts results in a pipe being longer than 25 meters.
{25, 50, 75}
50
Returns: 0.0
This time, it is impossible to cut the pipe such that some resulting pipe is longer than 50 meters.
{25, 50, 75}
24
Returns: 1.0
{99, 88, 77, 66, 55, 44, 33, 22, 11}
50
Returns: 0.7222222222222222
{2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98}
34
Returns: 0.9974489795918368
{2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98}
95
Returns: 0.002551020408163265
Probably the smallest return value greater than 0.0
{2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98}
60
Returns: 0.4362244897959184
random test
{50,51}
50
Returns: 0.0
{50,51}
49
Returns: 1.0
{67, 21, 90, 77, 19, 38, 85, 91, 23, 29, 42}
71
Returns: 0.18181818181818182
{2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98}
34
Returns: 0.9974489795918368
probably largest return value less than 1.0
{31, 55, 22, 99, 1, 49, 21, 7, 3, 88, 89, 36, 41}
77
Returns: 0.2948717948717949
{2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98}
55
Returns: 0.5892857142857143
{99,97,95,93,91,89,87,85,83,81,79,77,75,73,71,69,67,65,63,61,59,57,55,53,51,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98}
51
Returns: 0.9208163265306123
{99,97,95,93,91,89,87,85,83,81,79,77,75,73,71,69,67,65,63,61,59,57,55,53,51,50,52,54,56,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98}
60
Returns: 0.6048979591836735
{54,69,86,50,21,66,35,62,55,44,25,2,36,37,53,82,14,19,92,60,68,16,87,78,29,31,34,77,33,74,51,97,46,58,39,71,72,32,88,42}
74
Returns: 0.07307692307692308
{5,92,62,69,42,11,91,24,7,78,46,44,15,90,82,71,20,39,13,96,14,19,93,38,47,12,75,45,9,2,81,73,17,64,60,26,22,84,32,67,56,30,85,66,36}
86
Returns: 0.04141414141414142
{8,92,80,13,95,86,4,76,85,94,41,91,57,47}
75
Returns: 0.5274725274725275
{12,30,87,77,22,46,98,64,20,71,44,45,36,33,92,68,97}
61
Returns: 0.45588235294117646
{74,5,72,20,29,91,80,14,30,39,46,12,95,15,93,82,79,42,25,1,84,37,7,19,9,50,35,54,44,65,68,6,28,66,99,21}
69
Returns: 0.353968253968254
{18,21,20,13,92,73,80,97,27,77,16,89,7,4,65,26,1,14,25,54,28,40,34,55,86,29,98,41,2,75,94}
50
Returns: 0.8451612903225807
{12,43,73,82,3,85,7,67,66,6,21,71,96,28,14,1,36,60,62,61,94,9,79,84,50,65,80,76,44,59,87,88,32}
48
Returns: 0.8428030303030303
{51,5,19,50,55,4,33,95,57,32,13,75,21,16,42,44,14,94,96,1,53,30,88,63,25,64,83,59,20,34,99,29,54}
92
Returns: 0.028409090909090908
{36,53,89,55,63,17,85,70,60,24,58,78,14,97,52,91,64,50,44,15,4,95,46,40,82,71,88,56,77,12,41,34,75,67,57,87,2,65,69,96}
44
Returns: 0.8769230769230769
{27,40,67,21,57,75,42,93,46,78,54,29,53,74,22,44,13,97,95,39,63,70,65,81,14,82}
82
Returns: 0.018461538461538463
{33,96,73,72,50,76,49,41,22,39,34,6,30,65,14,40,7,43,27,99,8,78,87,36,32,15,45,23,77,20,56,35,53,31,54,66}
36
Returns: 0.9761904761904762
{87,49,75,41,34,9,77,1,89,66,18,58,72,31,54,43,27,60,40,38,84,86,63,70,95,69,79,26,8,55,97,17}
43
Returns: 0.8508064516129032
{85,16,97,8,52}
71
Returns: 0.5
{92,17,21,10,4,90,52,66,58,95,53,22,99,1,63}
61
Returns: 0.5428571428571428
{25, 50, 75 }
50
Returns: 0.0
{30, 60, 90 }
50
Returns: 0.6666666666666666
{99, 88, 77, 66, 55, 44, 33, 22, 11 }
50
Returns: 0.7222222222222222
{50, 75 }
49
Returns: 1.0
{1, 2 }
50
Returns: 1.0
{99, 1 }
98
Returns: 0.0
{80, 90, 10, 20 }
50
Returns: 1.0
{24, 49 }
50
Returns: 1.0
{20, 60, 70 }
45
Returns: 0.6666666666666666
{15, 45, 56 }
50
Returns: 0.3333333333333333
{25, 75 }
70
Returns: 0.0
{25, 50, 60 }
39
Returns: 1.0
{10, 30, 59, 1 }
59
Returns: 0.5
{25, 75 }
30
Returns: 1.0
{75, 76 }
74
Returns: 1.0
{25, 26 }
25
Returns: 1.0
{26, 40 }
25
Returns: 1.0