- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Max count of N using digits of M such that 2 and 5, and, 6 and 9 can be treated as same respectively

Max count is a count which is the maximum possible. Here we have given an integer N and a string of integer M. Our task is to return the maximum count of making the number N using the digits of the string of integer M. Also given that, we can be treated 2 and 5, and, 6 and 9 can be same respectively.

### Sample Examples

#### Input 1

N = 29 M = "2569783" Output 1: 2

**Explanation** − As 5 is the same as 2 and 6 is the same as 9 so we have a total of two ‘2’ and two ‘9’. Therefore, the max count of making the number N (29) using the digits of the string M (2596783) is 2.

#### Input 2

N = 999 M = 6666925 Output 2: 1

## Approach

Let’s discuss this approach step by step below-

First, we will create a function ‘maxCountOfN’ that will take the given string ‘M’ and the number ‘N’ as the parameter and will return the required integer ‘maxCount’ as the return value.

In the function, we will create a hashmap ‘mp’ to store the frequency of each digit of the string ‘M’.

We define a variable ‘len’ to store the size of the string ‘M’.

Traverse the string ‘M’ from index ‘i = 0’ to the less than equal to the ‘len’ and perform the following operation under this loop:

If we get a digit as ‘2’ we convert it to ‘5’.

If we get a digit as ‘6’ we convert it to ‘9’.

Count the frequency of each digit in the ‘mp’ map as a character-integer pair.

Create another hashmap ‘mpN’ to store the frequency of the digit of the number N

Traverse the number ‘N’ using the while loop till N is greater than 0 and perform the following operation under this loop −

Create an integer ‘rem’ to store the last element of the digit

Check if rem is 2 and convert it to 5

Check if rem is 6 and convert it to 9

Count the frequency of each digit in the ‘mpN’ map as a character-integer pair. I.e. storing int as char in the map as ‘mpN[rem + ‘0’]’.

Decrease N to N%10 to remove the last digit of a number

We create a variable ‘maxCount’ in which we store ‘INT_MAX’.

At last, we traverse the map ‘mpN’ to find the max count of N and perform the following operation under this loop −

Create variable ‘key’ in which we store the digit of the Number

Check whether the key is present in the map of the string or not, if it is not present means we are not able to create the number ‘N’ using digits of string ‘M’, we return ‘0’.

Create a variable ‘tempCount’ in which we store the value (divide the frequency of the digit from the string M with the frequency of the current digit of N).

In the maxCount we store the minimum of tempCount and maxCount because if every digit of number ‘N’ is present in the sting ‘M’ then only making digit ‘N’ is possible

Return maxCount

### Example

#include <bits/stdc++.h> using namespace std; int maxCountOfN(int N, string M){ map< char, int >mp; //created hashmap to store the frequency of each digit of //string int len = M.size(); // getting the size of the string // iterating string using for loop for(int i=0;i<len;i++){ if(M[i] == '2'){ M[i] = '5'; // replace 2 with 5 } else if(M[i] == '6'){ M[i] = '9'; // replace 6 with 9 } mp[M[i]]++; //count frequency of digit of string } // creating another hashmap to store the frequency of digit of the number N map<char, int>mpN; // iterating number 'N' using while loop while(N > 0){ int rem = N % 10; // Get the last digit as the remainder //Replace 2 with 5 if(rem == 2){ rem = 5; } //Replace 6 with 9 if(rem == 6){ rem = 9; } mpN[rem + '0']++; //count frequency of digit of number N = N / 10; } int maxCount = INT_MAX; //Trvaerse the hashmap of the number to get the maxCount for(auto el : mpN){ // Get the key which is a digit from the number N to be formed int key = el.first; // If the key is not present in the string M, then the number N cannot be formed if (!mp.count(key)) return 0; // Divide the frequency of the digit from the string M with the frequency of the current digit of N int tempCount = mp[key] / el.second; // Choose the minimum maxCount = min(maxCount, tempCount); } // returning the maximum count return maxCount; } // main function int main(){ int N = 29; // given number string M = "2569783";// given string // calling the function to get a maximum count of N int maxCount = maxCountOfN(N, M); cout<<"The max count of making the number "<< N << " using the digits of the string " << M << " is "<< maxCount<<endl; return 0; }

### Output

The max count of making the number 29 using the digits of the string 2569783 is 2

## Conclusion

In this tutorial, we have implemented a program to find the Max count of N using digits of M such that 2 and 5, and, 6 and 9 can be treated as the same respectively. We have implemented an approach of hashing as we have to store the frequency with the time complexity of O(N+M) and space complexity of O(N+M). Where M is the size of the string and N is the size of the Number.