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:
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] [6] [7] [8] [9]. 
Out of these, 3 groups have the largest size that is 2.

Input: N = 2 
Output:
Explanation: 
There are 2 groups in total, they are grouped according to the product of its digits 
of numbers from 1 to 2: [1] [2]. 
Out of these, 2 groups have the largest size that is 1. 

Approach: 
To solve the problem mentioned above we have to store the product of digit of every element from 1 to N using hash map and increment its frequency if it repeats. Then we have to find the maximum frequency within the hash map, which would be the largest size of the group. Finally, count all the groups who have the same frequency count as the largest group and return the count.

Below is the implementation of the above approach: 

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to Count the 
// groups having largest size while 
// grouping is according to 
// the product of its digits 
import java.io.*;
import java.util.*; 
  
class GFG{
  
// Function to find out product of digit 
static int digit_prod(int x) 
    int prod = 1
  
    // Calculate product 
    while (x != 0
    
        prod *= x % 10
        x = x / 10
    
  
    // Return the product of digits 
    return prod; 
  
// Function to find the count 
static int find_count(int n) 
      
    // Hash map for counting frequency 
    Map<Integer, Integer> mpp = new HashMap<>(); 
  
    for(int i = 1; i <= n; i++)
    
          
        // Counting freq of each element 
        int t = digit_prod(i);
        mpp.put(t, mpp.getOrDefault(t, 0) + 1);
    
  
    int ans = 1
    int maxm = 0
      
    for(Integer x : mpp.values())
    
          
        // Find the maximum 
        if (x > maxm) 
        
            maxm = x; 
            ans = 1
        
  
        else if (x == maxm) 
        {
              
            // Count the number of groups having 
            // size of equal to largest group. 
            ans++; 
        
    
    return ans; 
  
// Driver Code 
public static void main(String args[]) 
      
    // Initialise N 
    int N = 13
  
    System.out.print(find_count(N)); 
}
  
// This code is contributed by offbeat

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 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 
def digit_prod(x) :
    prod = 1
  
    # calculate product 
    while(x) : 
        prod = prod * (x % 10
        x = x // 10
      
    # return the product of digits 
    return prod 
  
# Function to find the count 
def find_count(n) :
      
    # hash map for 
    # counting frequency 
    mpp = {} 
  
    for i in range(1, n + 1): 
          
        # counting freq of each element 
        x = digit_prod(i)
  
        if x in mpp :
            mpp[x] += 1
        else :
            mpp[x] = 1
  
    ans = 1
    maxm = 0
  
    for value in mpp.values() : 
  
        # find the maximum 
        if (value > maxm) :
            maxm = value 
            ans = 1
        elif (value == maxm) :
              
            # count the number of groups having 
            # size of equal to largest group. 
            ans = ans + 1
  
    return ans 
  
# Driver code 
  
# initialise N 
N = 13
  
print(find_count(N))
  
# This code is contributed by Sanjit_Prasad

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to count the 
// groups having largest size while 
// grouping is according to 
// the product of its digits 
using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Text; 
  
class GFG{
      
// Function to find out product of digit 
static int digit_prod(int x) 
    int prod = 1; 
  
    // Calculate product 
    while (x != 0) 
    
        prod *= x % 10; 
        x = x / 10; 
    
  
    // Return the product of digits 
    return prod; 
  
// Function to find the count 
static int find_count(int n) 
      
    // Hash map for counting frequency 
    Dictionary<int,
               int> mpp = new Dictionary<int,
                                         int>(); 
  
    for(int i = 1; i <= n; i++)
    
          
        // Counting freq of each element 
        int t = digit_prod(i);
        if (mpp.ContainsKey(t))
        {
            mpp[t]++;
        }
        else
        {
            mpp[i] = 1;
        }
    
  
    int ans = 1; 
    int maxm = 0; 
      
    foreach(KeyValuePair<int, int> x in mpp)
    
          
        // Find the maximum 
        if (x.Value > maxm) 
        
            maxm = x.Value; 
            ans = 1; 
        
        else if (x.Value == maxm) 
        {
              
            // Count the number of groups having 
            // size of equal to largest group. 
            ans++; 
        
    
    return ans; 
}
      
// Driver Code
public static void Main(string[] args)
{
      
    // Initialise N 
    int N = 13; 
  
    Console.Write(find_count(N));
}
}
  
// This code is contributed by rutvik_56

chevron_right


Output: 

3

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.