Count of largest sized groups while grouping according to product of digits
Last Updated :
08 May, 2023
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: 3
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: 2
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++
#include <bits/stdc++.h>
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;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int digit_prod( int x)
{
int prod = 1 ;
while (x != 0 )
{
prod *= x % 10 ;
x = x / 10 ;
}
return prod;
}
static int find_count( int n)
{
Map<Integer, Integer> mpp = new HashMap<>();
for ( int i = 1 ; i <= n; i++)
{
int t = digit_prod(i);
mpp.put(t, mpp.getOrDefault(t, 0 ) + 1 );
}
int ans = 1 ;
int maxm = 0 ;
for (Integer x : mpp.values())
{
if (x > maxm)
{
maxm = x;
ans = 1 ;
}
else if (x == maxm)
{
ans++;
}
}
return ans;
}
public static void main(String args[])
{
int N = 13 ;
System.out.print(find_count(N));
}
}
|
Python3
def digit_prod(x) :
prod = 1
while (x) :
prod = prod * (x % 10 )
x = x / / 10
return prod
def find_count(n) :
mpp = {}
for i in range ( 1 , n + 1 ):
x = digit_prod(i)
if x in mpp :
mpp[x] + = 1
else :
mpp[x] = 1
ans = 1
maxm = 0
for value in mpp.values() :
if (value > maxm) :
maxm = value
ans = 1
elif (value = = maxm) :
ans = ans + 1
return ans
N = 13
print (find_count(N))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
class GFG{
static int digit_prod( int x)
{
int prod = 1;
while (x != 0)
{
prod *= x % 10;
x = x / 10;
}
return prod;
}
static int find_count( int n)
{
Dictionary< int ,
int > mpp = new Dictionary< int ,
int >();
for ( int i = 1; i <= n; i++)
{
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)
{
if (x.Value > maxm)
{
maxm = x.Value;
ans = 1;
}
else if (x.Value == maxm)
{
ans++;
}
}
return ans;
}
public static void Main( string [] args)
{
int N = 13;
Console.Write(find_count(N));
}
}
|
Javascript
<script>
function digit_prod(x) {
let prod = 1;
while (x != 0) {
prod = prod * (x % 10);
x = Math.floor(x / 10);
}
return prod;
}
function find_count(n) {
let mpp = new Map();
for (let i = 1; i <= n; i++) {
let t = digit_prod(i)
if (mpp.has(t)) {
mpp.set(t, mpp.get(t) + 1)
} else {
mpp.set(t, 1)
}
}
let ans = 1;
let maxm = 0;
for (let x of mpp) {
if (x[1] > maxm) {
maxm = x[1];
ans = 1;
}
else if (x[1] == maxm) {
ans++;
}
}
return ans;
}
let N = 13;
document.write(find_count(N));
</script>
|
Time complexity: O(nlog(d)), where n is the value of N and d is the number of digits in the largest number in the range
Space complexity: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...