Count Largest Group (LeetCode 1399): Solution and Explanation

Easy
Hash Table
Math
Counting

Group the numbers 1 to n by the sum of their digits and return how many groups are tied for the largest size.

Intuition

Each number belongs to exactly one group, named by its digit sum. So the task is a frequency count: how many numbers have digit sum 1, how many have 2, and so on.

The keys are tiny. With n at most 10^4, the largest digit sum is 9 + 9 + 9 + 9 = 36, so an array of 37 counters replaces a hash map.

Once every number is in its bucket, only the bucket sizes matter. Find the tallest one, then count how many buckets have that same height.

Approach: Digit-sum buckets

  1. Create count = [0] * 37.
  2. For i from 1 to n, add up the digits of i with % 10 and // 10, and increment count[s].
  3. Let best be the maximum of count.
  4. Return the number of entries equal to best.

Step-by-step example

n = 13
  1. 1 through 9 have digit sums 1 through 9, so each bucket 1..9 gets one number.
  2. 10 has digit sum 1 + 0 = 1, so bucket 1 grows to 2.
  3. 11, 12 and 13 have digit sums 2, 3 and 4, so buckets 2, 3 and 4 also grow to 2.
  4. The tallest bucket height is 2, and buckets 1, 2, 3 and 4 reach it. Return 4.

Code: Python, Java, C++

1class Solution:
2    def countLargestGroup(self, n: int) -> int:
3        count = [0] * 37
4        for i in range(1, n + 1):
5            s, x = 0, i
6            while x:
7                s += x % 10
8                x //= 10
9            count[s] += 1
10        best = max(count)
11        return count.count(best)
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Reuse earlier digit sums O(n)
Digit-sum buckets O(n log n)
Hash map of groups O(n log n)
Time

O(n log n)

Each of the n numbers has O(log n) digits to add up. The final scan over 37 counters is constant.

Space

O(1)

The 37 counters do not grow with n.

Other approaches

Reuse earlier digit sums (time O(n), space O(n))

digitSum(i) = digitSum(i / 10) + i % 10, so an array of digit sums can be filled in O(1) per number. That makes the whole thing O(n) time at the cost of O(n) memory.

Hash map of groups (time O(n log n), space O(n))

Store each group as a list in a dictionary keyed by digit sum, then compare list lengths. Correct, but it keeps every number around when only the counts are needed.

Common mistakes

  • Returning the size of the largest group instead of how many groups have that size.
  • Counting empty buckets. Bucket 0 and any digit sum that no number reaches have count 0; they only tie with best if best were 0, which cannot happen because n is at least 1.
  • Sizing the array too small. A fixed array of 10 or 28 counters overflows once n reaches numbers like 9999 with digit sum 36.
  • Starting the loop at 0, which adds an extra number to bucket 0.

Frequently asked questions

Why 37 counters?

The largest digit sum for any number up to 10^4 is 36, from 9999. Index 0 is unused, so 37 slots cover sums 0 through 36.

Can the answer be larger than 1 for big n?

Yes. For n = 1000 the digit sums 13 and 14 tie for the largest group, so the answer is 2. For many large n a single middle digit sum wins and the answer is 1.

Is there a closed-form answer?

Not a simple one. Counting numbers with a given digit sum below n is a digit DP problem, which is far more work than the direct count for n up to 10^4.