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