Given an integer **N**, the task is to find the number of groups having the largest size. Each number from **1 to N** is grouped according to the **sum of its digits**.

**Examples:**

Input:N = 13

Output:4

Explanation:

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

Out of these, 4 groups have the largest size that is 2.

Input:n = 2

Output:2

Explanation:

There are 2 groups in total. [1] [2] and both the groups have largest size 1.

**Approach:** To solve the problem mentioned above we need to create a dictionary whose key represents the unique sum of digits of numbers from 1 to N. The values of those keys will keep count how many numbers have the sum equal to its key. Then we will print the highest value among all of them.

**Below is the implementation of the above approach:**

## C++

`// C++ implementation to Count the ` `// number of groups having the largest ` `// size where groups are according ` `// to the sum of its digits ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to return sum of digits of i ` `int` `sumDigits(` `int` `n){ ` ` ` `int` `sum = 0; ` ` ` `while` `(n) ` ` ` `{ ` ` ` `sum += n%10; ` ` ` `n /= 10; ` ` ` `} ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Create the dictionary of unique sum ` `map<` `int` `,` `int` `> constDict(` `int` `n){ ` ` ` ` ` `// dictionary that contain ` ` ` `// unique sum count ` ` ` `map<` `int` `,` `int` `> d; ` ` ` ` ` `for` `(` `int` `i = 1; i < n + 1; ++i){ ` ` ` `// calculate the sum of its digits ` ` ` `int` `sum1 = sumDigits(i); ` ` ` ` ` `if` `(d.find(sum1) == d.end()) ` ` ` `d[sum1] = 1; ` ` ` `else` ` ` `d[sum1] += 1; ` ` ` `} ` ` ` ` ` `return` `d; ` `} ` ` ` `// function to find the ` `// largest size of group ` `int` `countLargest(` `int` `n){ ` ` ` ` ` `map<` `int` `,` `int` `> d = constDict(n); ` ` ` ` ` `int` `size = 0; ` ` ` ` ` `// count of largest size group ` ` ` `int` `count = 0; ` ` ` ` ` `for` `(` `auto` `it = d.begin(); it != d.end(); ++it){ ` ` ` `int` `k = it->first; ` ` ` `int` `val = it->second; ` ` ` ` ` `if` `(val > size){ ` ` ` `size = val; ` ` ` `count = 1; ` ` ` `} ` ` ` `else` `if` `(val == size) ` ` ` `count += 1; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 13; ` ` ` ` ` `int` `group = countLargest(n); ` ` ` ` ` `cout << group << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to Count the ` `# number of groups having the largest ` `# size where groups are according ` `# to the sum of its digits ` ` ` `# Create the dictionary of unique sum ` `def` `constDict(n): ` ` ` ` ` `# dictionary that contain ` ` ` `# unique sum count ` ` ` `d ` `=` `{} ` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` ` ` `# convert each number to string ` ` ` `s ` `=` `str` `(i) ` ` ` ` ` `# make list of number digits ` ` ` `l ` `=` `list` `(s) ` ` ` ` ` `# calculate the sum of its digits ` ` ` `sum1 ` `=` `sum` `(` `map` `(` `int` `, l)) ` ` ` ` ` `if` `sum1 ` `not` `in` `d: ` ` ` `d[sum1] ` `=` `1` ` ` ` ` `else` `: ` ` ` `d[sum1] ` `+` `=` `1` ` ` ` ` `return` `d ` ` ` `# function to find the ` `# largest size of group ` `def` `countLargest(n): ` ` ` ` ` `d ` `=` `constDict(n) ` ` ` ` ` `size ` `=` `0` ` ` ` ` `# count of largest size group ` ` ` `count ` `=` `0` ` ` ` ` `for` `k, val ` `in` `d.items(): ` ` ` ` ` `if` `val > size: ` ` ` ` ` `size ` `=` `val ` ` ` `count ` `=` `1` ` ` ` ` `elif` `val ` `=` `=` `size: ` ` ` ` ` `count ` `+` `=` `1` ` ` ` ` `return` `count ` ` ` `# Driver Code ` `n ` `=` `13` `group ` `=` `countLargest(n) ` `print` `(group) ` ` ` `# This code is contributed by Sanjit_Prasad ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Maximize number of groups formed with size not smaller than its largest element
- Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Number of ways of distributing N identical objects in R distinct groups with no groups empty
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Count of groups among N people having only one leader in each group
- Check if the sum of digits of number is divisible by all of its digits
- Sum of the digits of square of the given number which has only 1's as its digits
- Find the Largest number with given number of digits and sum of digits
- Sort the numbers according to their sum of digits
- Smallest N digit number with none of its digits as its divisor
- Python | Grouping dictionary keys by value
- Python | Grouping list values into dictionary
- Python - Frequency Grouping Dictionary
- Python - Vertical Grouping Value Lists
- Python - Convert List to key-value list by prefix grouping
- Check if a number is a perfect square having all its digits as a perfect square
- Largest number not greater than N which can become prime after rearranging its digits

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.