
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 N-Digit Numbers Not Having Given Prefixes
Here the problem is to determine the total number of strings of the length N containing characters '0' through '9' provided an integer N with an array of string prefixes pre[]. such that none of the strings can contain the provided prefixes. The aim of this article is to implement a program to find the count of N digit numbers not having given prefixes.
A collection of various strings is referred to as an array in the C programming language because an array is a linear grouping of data pieces of a similar type.
As we already know, the string is a character-by-character, one-dimensional array that ends with an empty or a null character.
Sample Example 1
Let us take the Input N = 2,
The given prefix, pre = {"1"}
Output obtained: 90
Explanation
All the 2 digit strings excluding {"01","10","11", "12", "13", "14", "15", "16", "17", "18", "19", "21", "31", "41", "51", "61", "71", "81", "91"} are valid here.
Sample Example 2
Let us take the Input N = 3,
The given prefix, pre = {"56"}
Output obtained: 990
Explanation
All the 3 digit strings excluding {"560", "561", "562", "563", "564", "565", "566", "567", "568", "569"} are valid here.
Sample Example 3
Let us take the Input N = 1,
The given prefix, pre = {"6"}
Output obtained: 9
Explanation
All the 1 digit strings excluding {"6"} are valid here.
Problem Statement
Implement a program to find the count of N digit numbers not having given prefixes.
Approach
In Order to find the count of N digit numbers not having given prefixes, we use the following methodology.
The approach to solve this problem and to find the count of N digit numbers not having given prefixes
Given that there are 10 character options for each position in a string, there are a total of (10N) potential strings. Instead of computing the total number of wanted strings, subtract the total number of unwanted strings. Merging prefixes with identical initial character as the longer prefix prior to iterating through the prefixes may result in the removal of certain repetitions.
Algorithm
The algorithm to to find the count of N digit numbers not having given prefixes given below
Step 1 ? Start
Step 2 ? Define function to calculate total strings of length N without the given prefixes
Step 3 ? Calculate the total strings present
Step 4 ? Make an array and counter a and aCount respectively and insert these prefixes with same
Step 5 ? Make a new array of prefixes strings
Step 6 ? Iterating for each of the starting character
Step 7 ? Iterate over the array to calculate minimum size prefix
Step 8 ? now take all these minimum prefixes in the new array of prefixes
Step 9 ? Iterating over the new prefixes
Step 10 ? Deduct the unwanted strings
Step 11 ? Print the result obtained
Step 12 ? Stop
Example: C Program
Here is the C program implementation of the above written algorithm to find the count of N digit numbers not having given prefixes.
#include <stdio.h> #include <math.h> #include <string.h> #define MAX_LENGTH 10 // Function to calculate total strings of length N without the given prefixes int totalStrings(int N, char pre[][MAX_LENGTH], int pre_Count){ // Calculate total strings present int total = (int)(pow(10, N) + 0.5); // Make an array and counter a and aCount respectively and insert these prefixes with same character in the array char a[10][MAX_LENGTH]; int aCount[10] = {0}; for (int i = 0; i < pre_Count; i++) { int index = pre[i][0] - '0'; strcpy(a[index] + aCount[index] * MAX_LENGTH, pre[i]); aCount[index]++; } // Make a new array of prefixes strings char new_pre[pre_Count][MAX_LENGTH]; int new_pre_count = 0; // Iterating for each of the starting //character for (int x = 0; x < 10; x++){ int m = N; // Iterate over the array to calculate minimum size prefix for (int j = 0; j < aCount[x]; j++){ int p_length = strlen(a[x] + j * MAX_LENGTH); m = (m < p_length) ? m : p_length; } // now take all these minimum prefixes in the new array of prefixes for (int j = 0; j < aCount[x]; j++){ int p_length = strlen(a[x] + j * MAX_LENGTH); if (p_length <= m){ strcpy(new_pre[new_pre_count], a[x] + j * MAX_LENGTH); new_pre_count++; } } } // Iterating through the new prefixes for (int i = 0; i < new_pre_count; i++){ // Subtract the unwanted strings total -= (int)(pow(10, N - strlen(new_pre[i])) + 0.5); } return total; } // The main function int main(){ int N = 5; char pre[][MAX_LENGTH] = {"1", "0", "2"}; int pre_Count = sizeof(pre) / sizeof(pre[0]); printf("%d\n", totalStrings(N, pre, pre_Count)); return 0; }
Output
70000
Conclusion
Likewise, we can to find the count of N digit numbers not having given prefixes.
The challenge of obtaining the program to find the count of N digit numbers not having given prefixes is resolved in this article.
Here C programming code as well as the algorithm to find the count of N digit numbers not having given prefixes are provided.