Given an integer n
, consider every number from 1
to n
(inclusive). For each number, calculate the sum of its digits. Group the numbers according to their digit sum. Your task is to count how many groups have the largest size. In other words, after grouping all numbers by their digit sum, find the size of the largest group(s), and return how many groups have that size.
Constraints:
1 <= n <= 10^4
1
to n
is used exactly once.
At first glance, the problem appears to require grouping numbers by their digit sums and then identifying the most populous groups. A brute-force approach would involve iterating through all numbers from 1
to n
, computing the digit sum for each, and tallying the frequencies of each group.
The main challenge is efficiently grouping the numbers and then determining which group(s) are largest. Using a data structure that allows for quick updates and queries (like a hash map) is key. Once all groups are formed, we need to find the maximum group size and count how many groups have that size.
The problem does not require us to list the numbers in each group—only to count the groups with the largest size. This simplifies our data structures and approach.
We can solve the problem efficiently by following these steps:
We use a hash map for constant-time lookups and updates as we process each number. Calculating the digit sum can be done in logarithmic time relative to the number's size, but for our constraints, this is efficient enough.
Let's take n = 13
as an example. We'll process numbers from 1 to 13:
Now, group sizes:
Output: 4
class Solution:
def countLargestGroup(self, n: int) -> int:
from collections import defaultdict
groups = defaultdict(int)
for num in range(1, n + 1):
digit_sum = sum(int(d) for d in str(num))
groups[digit_sum] += 1
max_size = max(groups.values())
return sum(1 for size in groups.values() if size == max_size)
class Solution {
public:
int countLargestGroup(int n) {
unordered_map<int, int> groups;
for (int num = 1; num <= n; ++num) {
int sum = 0, x = num;
while (x) {
sum += x % 10;
x /= 10;
}
groups[sum]++;
}
int max_size = 0, count = 0;
for (auto &p : groups) {
if (p.second > max_size) {
max_size = p.second;
count = 1;
} else if (p.second == max_size) {
count++;
}
}
return count;
}
};
class Solution {
public int countLargestGroup(int n) {
Map<Integer, Integer> groups = new HashMap<>();
for (int num = 1; num <= n; num++) {
int sum = 0, x = num;
while (x > 0) {
sum += x % 10;
x /= 10;
}
groups.put(sum, groups.getOrDefault(sum, 0) + 1);
}
int max = 0, count = 0;
for (int size : groups.values()) {
if (size > max) {
max = size;
count = 1;
} else if (size == max) {
count++;
}
}
return count;
}
}
var countLargestGroup = function(n) {
const groups = {};
for (let num = 1; num <= n; num++) {
let sum = 0, x = num;
while (x > 0) {
sum += x % 10;
x = Math.floor(x / 10);
}
groups[sum] = (groups[sum] || 0) + 1;
}
let max = 0, count = 0;
for (let key in groups) {
if (groups[key] > max) {
max = groups[key];
count = 1;
} else if (groups[key] === max) {
count++;
}
}
return count;
};
Brute-force approach:
The use of a hash map or array for counting groups keeps both time and space within acceptable limits for the problem's constraints.
This problem demonstrates a classic grouping task, where we classify numbers by a computed property (digit sum), then analyze the resulting groups. By using a hash map to tally group sizes, we ensure efficient updates and queries. The solution is elegant in its simplicity: process each number, track group sizes, and count the largest ones. No advanced data structures are needed, and the approach scales well for the given constraints.