# Count of alphabets whose ASCII values can be formed with the digits of N in C++

C++Server Side ProgrammingProgramming

Given a long variable containing a positive number as input. The goal is to find the count of alphabets whose ASCII value digits are present in the digits of the number.

Pick any two digits from the number and arrange them in a manner such that they form an ASCII value of English alphabets. ASCII values of A-Z start from 65 to 90 and ASCII values of a-z start from 97 to 122.

Total numbers that are to be picked will be 26+26=52.

## Let us understand with examples.

For Example

Input -  N_digits = 163465

Output - Count of alphabets whose ASCII values can be formed with the digits of N are: 2

Explanation - The ASCII values present in 163465 are 65 and 66 only. So only two alphabets are possible.

Input - N_digits = 902349

Output - Count of alphabets whose ASCII values can be formed with the digits of N are: 2

Explanation - The ASCII values present in 902349 are 90 and 99 only. So only two alphabets are possible.

## Approach used in the below program is as follows

In this approach we will first make a frequency array total[10] for storing the frequencies of digits in the input number. Now take each number i in the ranges 65 to 90 and 97 to 122. Extract digits of i and search in frequency array. If all digits of i are present in the frequency array ( total[ current digit] will be non-zero for all digits ) then increment count.

• Take long int N_digits as input.
• Function check(int arr[], int val) takes frequency array arr[] and ASCII number val as input and returns true if val could be made from digits in arr[].
• Using for loop make a copy of the frequency array as total[10].
• Now using a while loop, extract each digit of val and search in total[].
• If total[digit] is 0 then return false, otherwise use it and decrement its count by 1.
• Reduce val for next LSB.
• If while executes fully then val can be made from digits in total[] so return true.
• Function ASCII_N(long long int N_digits) takes the input number and returns the count of alphabets whose ASCII values can be formed with the digits of N.
• Take initial count as 0 and initialize frequency array total[10] with 0.
• Populate frequency array for digits in N_digits using while loop. Extract LSB as values = N_digits % 10 and increment  total[values] by 1.
• Reduce N_digits by 10.
• Now traverse ASCII numbers of alphabets from 97 to 122 and 65 to 90 using for loop.
• If any check(total, i) returns true then increment count.
• At the end of both for loops return count as result.

## Example

Live Demo

#include<bits/stdc++.h>
using namespace std;

bool check(int arr[], int val) {
int total[10];
for (int i = 0; i < 10; i++) {
total[i] = arr[i];
}
while (val > 0) {
int values = val % 10;
if (total[values] == 0) {
return false;
} else {
total[values]--;
}
val = floor(val / 10);
}
return true;
}

int ASCII_N(long long int N_digits) {
int count = 0;
int total[10] = {
0
};

while (N_digits > 0) {
int values = N_digits % 10;
total[values]++;
N_digits = floor(N_digits / 10);
}
for (int i = 97; i <= 122; i++) {
if (check(total, i)) {
count++;
}
}
for (int i = 65; i < 91; i++) {
if (check(total, i)) {
count++;
}
}
return count;
}
int main() {
long long int N_digits = 251326;
cout << "Count of alphabets whose ASCII values can be formed with the digits of N are: " << ASCII_N(N_digits);
}

If we run the above code it will generate the following output −

## Output

Count of alphabets whose ASCII values can be formed with the digits of N are: 2
Published on 29-Jan-2021 08:02:46