Problem Statement
On January 1, 2007, a confectioner made several candies.
On the last day of each month she allows her children to eat several of those candies.
The lifetime of a candy is the number of days between January 1 and the day the candy is eaten, inclusive. For example, the lifetime of a candy eaten on January 31 is 31, and the lifetime of a candy eaten on December 31 is 365 (note that 2007 wasn't a leap year).
You are given a
Definition
- Class:
- AverageCandyLifetime
- Method:
- getAverage
- Parameters:
- int[]
- Returns:
- double
- Method signature:
- double getAverage(int[] eatenCandies)
- (be sure your method is public)
Notes
- The year 2007 was not a leap year.
- The number of days in the months of 2007, in order, were 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 and 31.
- The returned value must be accurate to within a relative or absolute value of 1E-9.
Constraints
- eatenCandies will contain exactly 12 elements.
- Each element of eatenCandies will be between 0 and 1000, inclusive.
- The sum of all the elements in eatenCandies will be greater than 0.
Examples
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Returns: 60.5
One candy was eaten on January 31 and the other was eaten on March 31. The lifetimes of the candies are 31 and 31+28+31=90. The average lifetime is (31+90)/2=60.5.
{0, 1000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Returns: 59.0
All candies were eaten on February 28. The lifetime of each candy is 31+28=59, so the average candy lifetime is 59.0.
{0, 0, 0, 0, 0, 1, 0, 0, 0, 50, 0, 0}
Returns: 301.5882352941176
Most of the candies were eaten on October 31 (Halloween), and the lifetime of each of those candies is 304. The average lifetime is smaller than 304, because of a candy with lifetime 181, eaten on June 30.
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1000}
Returns: 365.0
Eating all candies on the New Year's Eve makes the maximum possible average lifetime.
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
Returns: 252.80769230769232
{902, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Returns: 31.0
{0, 914, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Returns: 59.0
{0, 0, 576, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Returns: 90.0
{0, 0, 0, 369, 0, 0, 0, 0, 0, 0, 0, 0}
Returns: 120.0
{0, 0, 0, 0, 996, 0, 0, 0, 0, 0, 0, 0}
Returns: 151.0
{0, 0, 0, 0, 0, 135, 0, 0, 0, 0, 0, 0}
Returns: 181.0
{0, 0, 0, 0, 0, 0, 741, 0, 0, 0, 0, 0}
Returns: 212.0
{0, 0, 0, 0, 0, 0, 0, 736, 0, 0, 0, 0}
Returns: 243.0
{0, 0, 0, 0, 0, 0, 0, 0, 405, 0, 0, 0}
Returns: 273.0
{0, 0, 0, 0, 0, 0, 0, 0, 0, 239, 0, 0}
Returns: 304.0
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 385, 0}
Returns: 334.0
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 687}
Returns: 365.0
{1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000}
Returns: 196.91666666666666
{1000, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Returns: 31.027972027972027
{0, 8, 33, 74, 132, 207, 298, 405, 529, 669, 826, 1000}
Returns: 291.79359005022724
{0, 0, 0, 0, 0, 0, 2, 11, 41, 134, 386, 1000}
Returns: 348.76111817026685
{0, 91, 181, 269, 356, 439, 519, 594, 665, 730, 789, 841}
Returns: 258.5823894775301
{1000, 245, 880, 676, 549, 945, 86, 987, 397, 793, 785, 408}
Returns: 190.7308734356857
{492, 547, 598, 647, 693, 0, 87, 167, 241, 310, 375, 435}
Returns: 171.3220818815331
{928, 967, 920, 937, 966, 938, 957, 999, 937, 951, 942, 924}
Returns: 197.00325532289284
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Returns: 31.0
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
Returns: 59.0
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}
Returns: 334.0
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
Returns: 365.0
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
Returns: 59.0
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
Returns: 196.91666666666666
{1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
Returns: 54.333333333333336
{2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
Returns: 54.0
{0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
Returns: 90.0
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
Returns: 60.5
{1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 }
Returns: 196.91666666666666
{1, 2, 3, 4, 5, 6, 3, 3, 4, 2, 3, 4 }
Returns: 206.675
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
Returns: 252.80769230769232
{555, 354, 485, 669, 118, 211, 669, 336, 999, 1000, 561, 972 }
Returns: 225.19656516091788
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100 }
Returns: 361.6930693069307