TopCoder Statistics - Problem Statement
I'm tackling the above practice problem, my solution seems to work for example 0 and example 1, though not the last two. I've even tried working out manually example 2 and still got my solution though I know I'm going wrong somewhere. Here is my code
Any guidance would be appreciated.import java.util.HashMap; import java.util.Map; public class AverageCandyLifetime { public static void main(String[] args){ int[] candiesEaten = {0, 0, 0, 0, 0, 1, 0, 0, 0, 50, 0, 0}; System.out.println(getAverage(candiesEaten)); } public static double getAverage(int[] eatenCandies){ int totalCandiesLifetime = 0; double numberOfTimesEaten = 0; Map<Integer, Integer>daysInMonth = new HashMap<Integer, Integer>(); daysInMonth.put(0, 31); daysInMonth.put(1, 28); daysInMonth.put(2, 31); daysInMonth.put(3, 30); daysInMonth.put(4, 31); daysInMonth.put(5, 30); daysInMonth.put(6, 31); daysInMonth.put(7, 31); daysInMonth.put(8, 30); daysInMonth.put(9, 31); daysInMonth.put(10, 30); daysInMonth.put(11, 31); for(int i = 0;i<eatenCandies.length;i++){ if(eatenCandies[i]!=0){ numberOfTimesEaten++; for(int j = 0;j<=i;j++){ totalCandiesLifetime += daysInMonth.get(j); } } } return totalCandiesLifetime / numberOfTimesEaten; } }