- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Count of alphabets whose ASCII values can be formed with the digits of N in C++
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
#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
- Related Articles
- Program to count number of unique binary search tree can be formed with 0 to n values in Python
- Find the count of numbers that can be formed using digits 3 and 4 only and having length at max N in C++
- Count characters in a string whose ASCII values are prime in C++
- Count of n digit numbers whose sum of digits equals to given sum in C++
- Count of alphabets having ASCII value less than and greater than k in C++
- Find the largest number that can be formed with the given digits in C++
- Count and Print the alphabets having ASCII value in the range [l, r] in C++
- Count numbers < = N whose difference with the count of primes upto them is > = K in C++
- Count and Print the alphabets having ASCII value not in the range [l, r] in C++
- n-th number whose sum of digits is ten in C++
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M in C++
- Count numbers whose difference with N is equal to XOR with N in C++
- Count numbers whose XOR with N is equal to OR with N in C++
- Find maximum number that can be formed using digits of a given number in C++
- Count Numbers with N digits which consists of even number of 0's in C++
