Open In App

Count of groups having largest size while grouping according to sum of its digits

Last Updated : 12 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:
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:
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;
}


Java




// Java implementation to Count the 
// number of groups having the largest 
// size where groups are according 
// to the sum of its digits
import java.util.HashMap;
import java.util.Map;
 
class GFG{
     
// Function to return sum of digits of i
public static int sumDigits(int n)
{
    int sum = 0;
    while(n != 0)
    {
        sum += n % 10;
        n /= 10;
    }
   
    return sum;
}
   
// Create the dictionary of unique sum 
public static HashMap<Integer, Integer> constDict(int n)
{
     
    // dictionary that contain 
    // unique sum count 
    HashMap<Integer, Integer> d = new HashMap<>();
     
    for(int i = 1; i < n + 1; ++i)
    {
         
        // Calculate the sum of its digits 
        int sum1 = sumDigits(i);
   
        if (!d.containsKey(sum1))
            d.put(sum1, 1);
        else
            d.put(sum1, d.get(sum1) + 1);
    }
    return d;
}
   
// Function to find the 
// largest size of group 
public static int countLargest(int n)
{
    HashMap<Integer, Integer> d = constDict(n); 
       
    int size = 0;
   
    // Count of largest size group 
    int count = 0;
 
    for(Map.Entry<Integer, Integer> it : d.entrySet())
    {
        int k = it.getKey();
        int val = it.getValue();
         
        if (val > size)
        {            
            size = val;
            count = 1;
        }
        else if (val == size)            
            count += 1;
    }
   
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 13;
    int group = countLargest(n); 
   
    System.out.println(group);
}
}
 
// This code is contributed by divyeshrabadiya07


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


C#




// C# implementation to Count the 
// number of groups having the largest 
// size where groups are according 
// to the sum of its digits
using System;
using System.Collections.Generic;
class GFG {
     
    // Function to return sum of digits of i
    static int sumDigits(int n)
    {
        int sum = 0;
        while(n != 0)
        {
            sum += n % 10;
            n /= 10;
        }
        
        return sum;
    }
        
    // Create the dictionary of unique sum 
    static Dictionary<int, int> constDict(int n)
    {
          
        // dictionary that contain 
        // unique sum count 
        Dictionary<int, int> d = new Dictionary<int, int>();
          
        for(int i = 1; i < n + 1; ++i)
        {
              
            // Calculate the sum of its digits 
            int sum1 = sumDigits(i);
        
            if (!d.ContainsKey(sum1))
                d.Add(sum1, 1);
            else
                d[sum1] += 1;
        }
        return d;
    }
        
    // Function to find the 
    // largest size of group 
    static int countLargest(int n)
    {
        Dictionary<int, int> d = constDict(n); 
            
        int size = 0;
        
        // Count of largest size group 
        int count = 0;
         
        foreach(KeyValuePair<int, int> it in d)
        {
            int k = it.Key;
            int val = it.Value;
              
            if (val > size)
            {            
                size = val;
                count = 1;
            }
            else if (val == size)            
                count += 1;
        }
        
        return count;
    }  
   
  // Driver code
  static void Main()
  {
    int n = 13;
    int group = countLargest(n); 
    Console.WriteLine(group);
  }
}
 
// This code is contributed by divyesh072019


Javascript




// JS implementation to Count the
// number of groups having the largest
// size where groups are according
// to the sum of its digits
 
 
// function to return sum of digits of i
function sumDigits(n){
    let sum = 0;
    while(n > 0)
    {
        sum += n%10;
        n = Math.floor(n / 10);
    }
 
    return sum;
}
 
// Create the dictionary of unique sum
function constDict( n){
     
    // dictionary that contain
    // unique sum count
    let d = {};
 
    for(var i = 1; i < n + 1; ++i){
        // calculate the sum of its digits
        var sum1 = sumDigits(i);
 
        if(!d.hasOwnProperty(sum1))
            d[sum1] = 1;
        else
            d[sum1] += 1;       
    }
 
    return d;
}
 
// function to find the
// largest size of group
function countLargest( n){
     
    let d = constDict(n);
     
    let size = 0;
 
    // count of largest size group
    let count = 0;
     
    for (let [k, val] of Object.entries(d))
    {
        k = parseInt(k)
        val = parseInt(val)
 
        if(val > size){           
            size = val;
            count = 1;
        }
        else if(val == size)           
            count += 1;
    }
 
    return count;
}
    
     
// Driver code
let n = 13;
 
let group = countLargest(n);
 
console.log(group);
 
 
// This code is contributed by phasing17


Output: 

4

 

Time Complexity: O(N)

Auxiliary Space: O(N)
 



Similar Reads

Count of largest sized groups while grouping according to product of digits
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 product of its digits.Examples: Input: N = 13 Output: 3 Explanation: There are 9 groups in total, they are grouped according to the product of its digits of numbers from 1 to 13: [1, 11] [2, 12] [3, 13] [4] [5] [
7 min read
Find the size of largest group where groups are according to the xor of digits
Given an integer N and the task is to find the size of the largest group in a range 1 to N, where two numbers belong to the same group if xor of its digits is the same. Examples: Input: N = 13 Output: 2 Explanation: There are 10 groups in total, they are grouped according to the xor of its digits of numbers from 1 to 13: [11] [1, 10] [2, 13] [3, 12
14 min read
Count of numbers in range [L, R] having sum of digits of its square equal to square of sum of digits
Given two integers L and R, the task is to find the count of numbers in range [L, R] such that the sum of digits of its square is equal to the square of sum of its digits, Example: Input: L = 22, R = 22Output: 1Explanation: 22 is only valid number in this range as sum of digits of its square = S(22*22) = S(484) = 16 and square of sum of its digits
11 min read
Count of Disjoint Groups by grouping points that are at most K distance apart
Given a 2D array arr[] and value K, where each list in arr[] represents a point on Cartesian coordinates, the task is to group the given points if the distance between them is less than or equal to K and find the total number of disjoint groups. Note: If points 'a' and 'b' are in the same group and 'a', 'd' are in the same group then 'b' and 'd' ar
11 min read
Maximize number of groups formed with size not smaller than its largest element
Given an array arr[] of N positive integers(1 ? arr[i] ? N ), divide the elements of the array into groups such that the size of each group is greater than or equal to the largest element of that group. It may be also possible that an element cannot join any group. The task is to maximize the number of groups. Examples: Input: arr = {2, 3, 1, 2, 2}
7 min read
Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups
Given N rows and M columns of a matrix, the task is to fill the matrix cells using first K integers such that: Each group of cells is denoted by a single integer.The difference between the group containing the maximum and the minimum number of cells should be minimum.All the cells of the same group should be contiguous i.e., for any group, two adja
11 min read
Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
Given three positive integers N, A, and B. The task is to count the numbers of length N containing only digits A and B and whose sum of digits also contains the digits A and B only. Print the answer modulo 109 + 7.Examples: Input: N = 3, A = 1, B = 3 Output: 1 Possible numbers of length 3 are 113, 131, 111, 333, 311, 331 and so on... But only 111 i
15 min read
Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
Given a range [L, R] and two positive integers N and M. The task is to count the numbers in the range containing only non-zero digits whose sum of digits is equal to N and the number is divisible by M.Examples: Input: L = 1, R = 100, N = 8, M = 2 Output: 4 Only 8, 26, 44 and 62 are valid numbers Input: L = 1, R = 200, N = 4, M = 11 Output: 2 Only 2
14 min read
Numbers with sum of digits equal to the sum of digits of its all prime factor
Given a range, the task is to find the count of the numbers in the given range such that the sum of its digit is equal to the sum of all its prime factors digits sum.Examples: Input: l = 2, r = 10 Output: 5 2, 3, 4, 5 and 7 are such numbers Input: l = 15, r = 22 Output: 3 17, 19 and 22 are such numbers As, 17 and 19 are already prime. Prime Factors
13 min read
Find the index having sum of elements on its left equal to reverse of the sum of elements on its right
Given an array, arr[] size N, the task is to find the smallest index of the array such that the sum of all the array elements on the left side of the index is equal to the reverse of the digits of the sum of all the array elements on the right side of that index. If no such index is found then print -1. Examples: Input: arr[] = { 5, 7, 3, 6, 4, 9,
15+ min read
three90RightbarBannerImg