

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Print all numbers less than N with at-most 2 unique digits in C++
In this problem, we are given an integer N and we have printed all the number less than N with at-most 2 unique digits i.e. maximum 2 different digits can be used to create the number.
Let’s take an example to understand the problem −
Input: N = 17 Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
To solve this problem, we will be generating all numbers that have only two unique digits. Our number generating process starts from 0 and ends when our number is equal to or greater than N. For two unique chosen, recursively generate numbers using num*10+i and num*10+j. There may arise some duplicate numbers in this process. So, we can use set to store numbers to avoid it.
Example
This program shows the implementation of our approach to solve the problem
#include <bits/stdc++.h> using namespace std; set<int> numbers; void generateNumbers(int n, int num, int i, int j){ if (num > 0 && num < n) numbers.insert(num); if (num >= n) return; if (num*10+i > num) generateNumbers(n, num*10+i, i, j); generateNumbers(n, num*10+j, i, j); } void printUniqueBitNumber(int n){ for (int i = 0; i <= 9; i++) for (int j = i + 1; j <= 9; j++) generateNumbers(n, 0, i, j); cout<<"The numbers are generated are : "; while (!numbers.empty()) { cout<<*numbers.begin()<<" "; numbers.erase(numbers.begin()); } } int main(){ int n = 17; printUniqueBitNumber(n); return 0; }
Output
The numbers are generated are : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
- Related Questions & Answers
- Print all prime numbers less than or equal to N in C++
- Counting n digit Numbers with all unique digits in JavaScript
- Print all Semi-Prime Numbers less than or equal to N in C++
- Recursive program to print all numbers less than N which consist of digits 1 or 3 only in C++
- Find all factorial numbers less than or equal to n in C++
- Find maximum product of digits among numbers less than or equal to N in C++
- Count Numbers with Unique Digits in C++
- Numbers At Most N Given Digit Set in C++
- Largest number less than X having at most K set bits in C++
- MySQL query to add 0's to numbers with less than 9 digits?
- Count all the numbers less than 10^6 whose minimum prime factor is N C++
- Print a number strictly less than a given number such that all its digits are distinct in C++
- Print all Prime Quadruplet of a number less than it in C++
- How to replace vector values less than 2 with 2 in an R vector?
- Count ordered pairs with product less than N in C++
Advertisements