import java.util.*;
public class Main {
public static void main(String[] args) {
List<Long> result = waysToChooseSum(200L, 210L);
System.out.println(result);
}
public static List<Long> waysToChooseSum(long lowLimit, long highLimit) {
// Create a list to store the result i.e total number of ways to choose sum so that max possible participants win the lottery
List<Long> finalResult = new ArrayList<>();
// Create a list to store Sum of digits of each number
List<Long> listDigit = new ArrayList<>();
// Loop through the lowLimit and highLimit to get the List of Sum of digits of each number
for (Long i = lowLimit; i <= highLimit; i++) {
Long j = i;
Long sum = 0L;
while (j != 0) {
sum = sum + j % 10;
j /= 10L;
}
listDigit.add(sum);
}
// Create a HashMap to store the number of times each sum appears in the listDigit array
// The key would be each unique number in listDigit and the value would be the number of times it appears in the listDigit array
Map<Long,Long> map = new HashMap<>();
for(Long i: listDigit) {
if(map.containsKey(i)) {
map.put(i, map.get(i) + 1L);
}else {
map.put(i, 1L);
}
}
// The max number of winners will be the maximum value in the map
Long winners = Collections.max(map.values());
// To get the number of winners, count the number of keys that have the maximum values in the map
Long ways = 0L;
for (Long sum : map.values()) {
if(Objects.equals(sum, winners)){
ways++;
}
}
finalResult.add(ways);
finalResult.add(winners);
return finalResult;
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: