Lottery Coupons

发布时间 2023-04-11 23:35:16作者: createMan

There is a lottery with n coupons and n people take part in it. Each person picks exactly one coupon. Coupons are numbered consecutively from 1 to n. A lottery winner is any person who owns a coupon where the sum of the digits on the coupon is equal to s. If there are multiple winners, the prize is split equally among them. Determine how many values of s there are where there is at least one winner and the prize is split among the largest group of people.

Example

n = 12

The list of coupon numbers generated from 1 to n is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]. The sums of the digits are [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3]. The largest number of winners is 2 which occurs for coupons [1, 10], [2, 11], and [3, 12]. The maximum possible number of winners occurs for any of these 3 possible values of s, so 3 is the answer.

Function Description

Complete the function lotteryCoupons in the editor.

lotteryCoupons has the following parameter(s):

int n: the maximum coupon number

Returns

int: the number of ways to choose s

Constraints

  • 1 ≤ n ≤ 104
Input Format For Custom Testing

The first line contains an integer, n, the maximum ticket number.

Sample Case 0

Sample Input

STDIN     Function 
-----     --------
3      →  n = 3

 

Sample Output

3

 

Explanation

Since all coupon numbers are different, i.e. (1, 2, 3), there can only be one winner. There are three possible winners.

Sample Case 1

Sample Input

STDIN     Function
-----     --------
11     →  n = 11

 

Sample Output

2

 

Explanation

The sums of the digits are 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, and 2. Winners have coupons (1, 10) or (2, 11). 

 

Sample Case 2
Sample Input
STDIN     Function
-----     --------
22     →  n = 22
Sample Output
3

Explanation

The lottery coupons are numbered from 1 to 22 and the sum of the digits of each of them is 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3 and 4  respectively. There are two ways to choose s:

  • When s = 1, winners are 1 and 10. 
  • When s = 2, winners are 2, 11, and 20.
  • When s = 3, then 3, 12, and 21.
  • When s = 4, then 4, 13, and 22.
  • There are two winning numbers when s is in the range [5-9].
  • When s = 10, there is one winning number, 19.

 

There are 3 ways to have the greatest number of winners, when n = 2, 3, or 4. 

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

public class Result {
    /*
     * Complete the 'lotteryCoupons' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts INTEGER n as parameter.
     */

    public static int lotteryCoupons(int n) {
        int[] arr = new int[n];
        List<Integer> list = new ArrayList<>();

        for (int i = 1; i <= n; i++) {
            int sum = 0;
            String[] strs = String.valueOf(i).split("");
            for (String str : strs) {
                sum += Integer.parseInt(str);
            }
            arr[i - 1] = sum;
        }

        int count = Arrays.stream(arr).max().getAsInt();
        for (int j = 1; j <= count; j++) {
            int val = 0;
            for (int k : arr) {
                if (j == k) {
                    val++;
                }
            }
            list.add(val);
        }
        int maxBuffer = 0;
        Integer max = Collections.max(list);
        for (Integer integer : list) {
            if (max == integer.intValue()) {
                maxBuffer++;
            }
        }
        return maxBuffer;
    }
}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        int result = Result.lotteryCoupons(n);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}