Count Largest Group - Problem

You are given an integer n, and your task is to organize numbers from 1 to n into groups based on an interesting property: the sum of their digits.

For example, consider n = 24:

  • Number 14 has digit sum: 1 + 4 = 5
  • Number 23 has digit sum: 2 + 3 = 5
  • Number 5 has digit sum: 5

So numbers 14, 23, and 5 would all belong to the same group (digit sum = 5).

Your goal is to find how many groups have the maximum size. In other words, find the largest group size, then count how many groups have exactly that many elements.

Input: An integer n (1 โ‰ค n โ‰ค 104)

Output: The number of groups that contain the maximum number of elements

Input & Output

example_1.py โ€” Basic case
$ Input: n = 13
โ€บ Output: 4
๐Ÿ’ก Note: Groups: {1:[1,10]}, {2:[2,11]}, {3:[3,12]}, {4:[4,13]}, {5:[5]}, {6:[6]}, {7:[7]}, {8:[8]}, {9:[9]}. All groups except {5},{6},{7},{8},{9} have size 2, but these 5 groups have size 1. So there are 4 groups with the maximum size of 2.
example_2.py โ€” Larger case
$ Input: n = 2
โ€บ Output: 2
๐Ÿ’ก Note: Groups: {1:[1]}, {2:[2]}. Both groups have size 1, which is the maximum. So answer is 2.
example_3.py โ€” Edge case
$ Input: n = 1
โ€บ Output: 1
๐Ÿ’ก Note: Only one group: {1:[1]} with size 1. There's 1 group with the maximum size of 1.

Constraints

  • 1 โ‰ค n โ‰ค 104
  • All numbers are positive integers
  • Maximum digit sum is 36 (for 9999)

Visualization

Tap to expand
Digital Library: Grouping by Digit SumShelf: Sum=1Books: 1,10Count: 2Shelf: Sum=2Books: 2,11,20Count: 3 โญShelf: Sum=3Books: 3,12Count: 2๐Ÿ“Š Analysis ResultsMaximum books per shelf: 3Number of shelves with maximum books: 1๐Ÿ’ก Key Insight: We only need to count shelf sizes, not track individual books
Understanding the Visualization
1
Scan Books
Go through each book and calculate the digit sum of its page count
2
Place on Shelves
Group books with the same digit sum together on the same shelf
3
Find Fullest Shelves
Identify which shelves have the most books and count how many such shelves exist
Key Takeaway
๐ŸŽฏ Key Insight: Use a hash table to count frequencies in one pass, then find how many groups share the maximum frequency.
Asked in
Amazon 15 Microsoft 8 Google 5
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen