import java.util.*;
import java.lang.*;
import java.io.*;
// The main method must be in a class named "Main".
class Main {
public static void main(String[] args) {
// Set lowLimit and highLimit
Long lowLimit = 1L;
Long highLimit = 15L;
// Create a list to store the result i.e total number of ways to choose sum so that max possible participants win the lottery and the number of winners
List<Long> finalResult = new ArrayList<>();
// Create a HashMap to store the number of times each sum appears
Map<Long,Long> map = new HashMap<>();
// Loop through the lowLimit and highLimit to get the List of Sum of digits of each number
for (Long i = lowLimit; i <= highLimit; i++) {
// Create a Long j to store current sumber i as we need to move to the next number in nex iteration
Long j = i;
// Create a Long sum to store the sum of current number
Long sum = 0L;
// To get each digit, loop through and divide the number by 10 and the remainder is the digit. Sum the digit to get the sum of all digits
while (j != 0) {
sum = sum + j % 10;
j /= 10L;
}
// Check the map keys to see if the sum exists, if exists, add 1 to the value. Else create a new key to store the sum and set 1 as the value
if(map.containsKey(sum)) {
map.put(sum, map.get(sum) + 1L);
}else {
map.put(sum, 1L);
}
}
System.out.println("Map: " + map);
// The max number of winners will be the maximum value in the map
Long winners = Collections.max(map.values());
System.out.println("Maximum Number of winners: " + winners);
// To get the number of ways to win, 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++;
}
}
System.out.println("Number of ways to win: " + ways);
finalResult.add(ways);
finalResult.add(winners);
System.out.println("Result: " + finalResult);
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: