Count of groups having largest size while grouping according to sum of its digits
Last Updated :
12 Sep, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int sumDigits( int n){
int sum = 0;
while (n)
{
sum += n%10;
n /= 10;
}
return sum;
}
map< int , int > constDict( int n){
map< int , int > d;
for ( int i = 1; i < n + 1; ++i){
int sum1 = sumDigits(i);
if (d.find(sum1) == d.end())
d[sum1] = 1;
else
d[sum1] += 1;
}
return d;
}
int countLargest( int n){
map< int , int > d = constDict(n);
int size = 0;
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;
}
int main()
{
int n = 13;
int group = countLargest(n);
cout << group << endl;
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
class GFG{
public static int sumDigits( int n)
{
int sum = 0 ;
while (n != 0 )
{
sum += n % 10 ;
n /= 10 ;
}
return sum;
}
public static HashMap<Integer, Integer> constDict( int n)
{
HashMap<Integer, Integer> d = new HashMap<>();
for ( int i = 1 ; i < n + 1 ; ++i)
{
int sum1 = sumDigits(i);
if (!d.containsKey(sum1))
d.put(sum1, 1 );
else
d.put(sum1, d.get(sum1) + 1 );
}
return d;
}
public static int countLargest( int n)
{
HashMap<Integer, Integer> d = constDict(n);
int size = 0 ;
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;
}
public static void main(String[] args)
{
int n = 13 ;
int group = countLargest(n);
System.out.println(group);
}
}
|
Python3
def constDict(n):
d = {}
for i in range ( 1 , n + 1 ):
s = str (i)
l = list (s)
sum1 = sum ( map ( int , l))
if sum1 not in d:
d[sum1] = 1
else :
d[sum1] + = 1
return d
def countLargest(n):
d = constDict(n)
size = 0
count = 0
for k, val in d.items():
if val > size:
size = val
count = 1
elif val = = size:
count + = 1
return count
n = 13
group = countLargest(n)
print (group)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int sumDigits( int n)
{
int sum = 0;
while (n != 0)
{
sum += n % 10;
n /= 10;
}
return sum;
}
static Dictionary< int , int > constDict( int n)
{
Dictionary< int , int > d = new Dictionary< int , int >();
for ( int i = 1; i < n + 1; ++i)
{
int sum1 = sumDigits(i);
if (!d.ContainsKey(sum1))
d.Add(sum1, 1);
else
d[sum1] += 1;
}
return d;
}
static int countLargest( int n)
{
Dictionary< int , int > d = constDict(n);
int size = 0;
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;
}
static void Main()
{
int n = 13;
int group = countLargest(n);
Console.WriteLine( group );
}
}
|
Javascript
function sumDigits(n){
let sum = 0;
while (n > 0)
{
sum += n%10;
n = Math.floor(n / 10);
}
return sum;
}
function constDict( n){
let d = {};
for ( var i = 1; i < n + 1; ++i){
var sum1 = sumDigits(i);
if (!d.hasOwnProperty(sum1))
d[sum1] = 1;
else
d[sum1] += 1;
}
return d;
}
function countLargest( n){
let d = constDict(n);
let size = 0;
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;
}
let n = 13;
let group = countLargest(n);
console.log(group);
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...