- 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
Numbers At Most N Given Digit Set in C++
Suppose we have one sorted set of digits D, a non-empty subset of {'1', '2', '3', '4', '5', '6', '7', '8', '9'} except 0. Now, we will write some numbers using these digits, using each digit as many times as we want. So, if D = {'2','3','7'}, we may write numbers such as '23', '771', '2372327'.
Now we have to find the number of positive integers that can be written that are less than or equal to N.
So, if the input is like D = [2,3,4,7], N = 100, then the output will be 20, as the numbers can be 2, 3, 4, 7, 22, 23, 24, 27, 32, 33, 34, 37, 42, 43, 44, 47, 72, 73, 74, 77. All other numbers are greater than 100.
To solve this, we will follow these steps −
n := convert N to string
sz := size of n, ret := 0
for initialize i := 1, when i < sz, update (increase i by 1), do −
ret := ret + (size of D)^i
for initialize i := 0, when i < sz, update (increase i by 1), do −
hasSameNum := false
for each string x in D −
if x[0] < n[i], then −
ret := ret + (size of D)^(sz - i - 1)
otherwise when x[0] is same as n[i], then −
hasSameNum := true
if not hasSameNum is non-zero, then −
return ret
return ret + 1
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int atMostNGivenDigitSet(vector<string> &D, int N) { string n = to_string(N); int sz = n.size(); int ret = 0; for (int i = 1; i < sz; i++) { ret += pow(D.size(), i); } for (int i = 0; i < sz; i++) { bool hasSameNum = false; for (string &x : D) { if (x[0] < n[i]) { ret += pow(D.size(), sz - i - 1); } else if (x[0] == n[i]) { hasSameNum = true; } } if (!hasSameNum) return ret; } return ret + 1; } }; main(){ Solution ob; vector<string> v = {"2","3","4","7",}; cout << (ob.atMostNGivenDigitSet(v, 100)); }
Input
{"2","3","4","7"}, 100
Output
20
- Related Articles
- Count n digit numbers divisible by given number in C++
- Largest N digit number divisible by given three numbers in C++
- Count all possible N digit numbers that satisfy the given condition in C++
- Count n digit numbers not having a particular digit in C++
- Number of n digit stepping numbers in C++
- Count numbers (smaller than or equal to N) with given digit sum in C++
- Print all numbers less than N with at-most 2 unique digits in C++
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Count of n digit numbers whose sum of digits equals to given sum in C++
- Print all n-digit strictly increasing numbers in C++
- Largest Even and Odd N-digit numbers in C++
- Count of Binary Digit numbers smaller than N in C++
- Count numbers with unit digit k in given range in C++
- Smallest number formed by shuffling one digit at most in JavaScript
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
