Count of groups having largest size while grouping according to sum of its digits | C ++ 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 ; Create the dictionary of unique sum ; dictionary that contain unique sum count ; calculate the sum of its digits ; function to find the largest size of group ; count of largest size group ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ; }