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:
4
💡 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). Four groups have this maximum size 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:
6
💡 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.
Constraints
- 1 ≤ n ≤ 104
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code