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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code