Count of largest sized groups while grouping according to product of digits | C ++ implementation to Count the groups having largest size while grouping is according to the product of its digits ; Function to find out product of digit ; calculate product ; return the product of digits ; Function to find the count ; hash map for counting frequency ; counting freq of each element ; find the maximum ; count the number of groups having size of equal to largest group . ; Driver code ; initialise N
#include <bits/stdc++.h> NEW_LINE using namespace std ; int digit_prod ( int x ) { int prod = 1 ; while ( x ) { prod *= x % 10 ; x = x / 10 ; } return prod ; } int find_count ( int n ) { map < int , int > mpp ; for ( int i = 1 ; i <= n ; i ++ ) { mpp [ digit_prod ( i ) ] += 1 ; } int ans = 1 ; int maxm = 0 ; for ( auto x : mpp ) { if ( x . second > maxm ) { maxm = x . second ; ans = 1 ; } else if ( x . second == maxm ) { ans ++ ; } } return ans ; } int main ( ) { int N = 13 ; cout << find_count ( N ) ; return 0 ; }