Largest Digit Sum Group Finder

Count Largest Group — Group Numbers by Digit Sum

Problem Statement

You're given a positive integer n.
Group all numbers from 1 to n based on the sum of their digits.

Your task is to find how many groups have the largest size (i.e., most numbers).

Input: n = 13 

Output: 4 

Explanation: 

There are 9 groups in total, they are grouped according sum of its digits of numbers from 1 to 13: [1,10], [2,11], [3,12], [4,13], [5], [6], [7], [8], [9]. There are 4 groups with largest size. 

Best Technique – Digit Grouping with Hashing / Array Optimization

This problem looks simple, but under the hood, it tests your ability to:

  • Understand digit operations

  • Use maps or arrays efficiently

  • Find maximums intelligently

Let’s explore multiple ways to solve this, from brute force to optimal, and see how each one helps us understand the problem better.

Different Approaches:

Approach 1: Brute Force using List Grouping

Group every number by the digit sum and store full lists of numbers for each group.
Then just check which list is the biggest.

import java.util.*; class Solution { public int countLargestGroup(int n) { Map<Integer, List<Integer>> map = new HashMap<>(); for (int i = 1; i <= n; i++) { int sum = digitSum(i); map.putIfAbsent(sum, new ArrayList<>()); map.get(sum).add(i); } int maxSize = 0; for (List<Integer> group : map.values()) { maxSize = Math.max(maxSize, group.size()); } int count = 0; for (List<Integer> group : map.values()) { if (group.size() == maxSize) { count++; } } return count; } private int digitSum(int num) { int sum = 0; while (num > 0) { sum += num % 10; num /= 10; } return sum; } }

Approach 2: HashMap with Count (More Efficient)

Don’t store actual numbers, just count how many fall into each digit sum group.

import java.util.*; class Solution { public int countLargestGroup(int n) { Map<Integer, Integer> group = new HashMap<>(); for (int i = 1; i <= n; i++) { int sum = digitSum(i); group.put(sum, group.getOrDefault(sum, 0) + 1); } int max = 0; for (int size : group.values()) { max = Math.max(max, size); } int count = 0; for (int size : group.values()) { if (size == max) { count++; } } return count; } private int digitSum(int num) { int sum = 0; while (num > 0) { sum += num % 10; num /= 10; } return sum; } }

Approach 3: Array Optimization (Most Optimal)

Use an array instead of HashMap since digit sums range only from 1 to 36.

class Solution { public int countLargestGroup(int n) { int[] group = new int[37]; // max digit sum = 36 for (int i = 1; i <= n; i++) { group[digitSum(i)]++; } int max = 0; for (int size : group) { max = Math.max(max, size); } int count = 0; for (int size : group) { if (size == max) { count++; } } return count; } private int digitSum(int num) { int sum = 0; while (num > 0) { sum += num % 10; num /= 10; } return sum; } }

⚠️Common Pitfalls

  • Forgetting to include n (loop from 1 to n, not 1 to n-1)

  • Using digitSum(n) instead of digitSum(i) inside the loop

  • Assuming HashMap will always give sorted keys — it won’t

📊Time and Space Complexity

Approach      Time Complexity     Space Complexity
Brute Force      O(n²)     O(n)
HashMap Count      O(n)     O(n)
Array Optimized      O(n)     O(1)

     

🌍Real-World Applications

  • Grouping IDs or codes by properties like checksum or digit sum

  • Hash functions often use digit-based reductions in hashing systems

  • Helpful in classification tasks where digit-based rules apply


I hope you found this article insightful! Keep exploring, keep learning, and don’t forget to check back daily for more exciting problem breakdowns. If you know someone who would benefit from this, share it with them and help them grow too! That’s it for today—see you in the next post!

                                                                                                                                         Signing off!! 

                                                                                                Master DSA One Problem at a Time :)

Comments

Popular posts from this blog

Trailing Zeros in Factorial – How Many and Why?

Divisible Drama — Who’s In, Who’s Out?

Zero Array After Queries