My first instinct was to just iterate up to n and tally digit sums.
Recognize that brute force is infeasible for n up to 1e9, so use digit DP to count numbers with each possible digit sum, then find the maximum count and how many sums achieve it. The maximum digit sum is 9 * number of digits, so the DP state is (position, sum, tight) and we aggregate counts per sum.
Pro tip: Mention that the answer can be computed without enumerating all numbers, and that digit DP is a standard technique for such counting problems. Also, note that the maximum group size is often achieved by multiple digit sums, so you must count all sums that tie for the maximum.
Clarify that n can be up to 1e9, so O(n) is too slow. The goal is to count how many digit sums have the maximum frequency among numbers 1 to n.
Since we need to count numbers by digit sum without iterating, use digit DP to compute the frequency of each possible digit sum efficiently.
Use state (position, current_sum, tight) where tight indicates if the prefix matches n. Transition by trying digits 0-9, updating sum, and adjusting tight. Memoize to avoid recomputation.
After DP, we have counts for each digit sum from 1 to 9*len(n). Find the maximum count and count how many sums achieve it.
Exclude 0 from the count (since range is 1 to n). Return the number of digit sums that have the maximum frequency.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.