Count Largest Group - Problem

You are given an integer n.

We need to group the numbers from 1 to n according to the sum of their digits. For example, the numbers 14 and 5 belong to the same group (digit sum = 5), whereas 13 and 3 belong to different groups (digit sums 4 and 3).

Return the number of groups that have the largest size, i.e. the maximum number of elements.

Input & Output

Example 1 — Basic Case
$ Input: n = 13
Output: 1
💡 Note: Numbers 1-13 grouped by digit sum: {1:[1,10], 2:[2,11], 3:[3,12], 4:[4,13], 5:[5], 6:[6], 7:[7], 8:[8], 9:[9]}. Groups with digit sum 1,2,3,4 each have 2 elements (maximum), but only digit sum 4 has the maximum count of 2.
Example 2 — Small Input
$ Input: n = 2
Output: 2
💡 Note: Numbers: {1:[1], 2:[2]}. Each group has size 1, which is the maximum. Two groups have this maximum size.
Example 3 — Larger Input
$ Input: n = 15
Output: 1
💡 Note: Groups: {1:[1,10], 2:[2,11], 3:[3,12], 4:[4,13], 5:[5,14], 6:[6,15], 7:[7], 8:[8], 9:[9]}. Maximum group size is 2, and 6 groups have this size, but we return 6.

Constraints

  • 1 ≤ n ≤ 104

Visualization

Tap to expand
Count Largest Group: n = 13Step 1: Numbers 1 to 13123... 13Step 2: Group by digit sumSum=1: [1,10]Sum=2: [2,11]Sum=3: [3,12]Sum=4: [4,13]Others: size 1Step 3: Count largest groupsMaximum group size = 2, appears in 4 groupsOutput: 4
Understanding the Visualization
1
Input
Given integer n=13, consider numbers 1 to 13
2
Group by Digit Sum
Group numbers with same digit sum together
3
Count Result
Find maximum group size and count how many groups have it
Key Takeaway
🎯 Key Insight: Group numbers by their digit sum, then count how many groups achieve the maximum size
Asked in
Google 15 Amazon 12 Microsoft 8
28.4K Views
Medium Frequency
~12 min Avg. Time
423 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