package codewars;//--https://www.codewars.com/kata/55f5efd21ad2b48895000040/train/javaimport java.util.*;public class MaxSumDigits{ private static Boolean valid(long num, int maxsm){ String str = String.valueOf(num); for(int i = 0; i <= str.length() - 4; i++){ int sum = 0; for(int j = i; j < i+4; j++){ sum += str.charAt(j) - '0'; if(sum > maxsm){ return false; } } } return true; } public static long[] maxSumDig(long nmax, int maxsm){ Listlist = new ArrayList (); for(long i = 1000; i <= nmax; i++){ if(MaxSumDigits.valid(i, maxsm)){ list.add(i); } } long sum = 0; for(Long x : list){ sum += x; } long mean = sum / list.size(); long nearest = 0; if(list.contains(means)){ nearest = mean; }else{ for(int i=0; i mean - list.get(i - 1) ? list.get(i - 1) : temp; break; } } } return new long[]{list.size(), nearest, sum}; }}