
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Counting n digit Numbers with all unique digits in JavaScript
- Print all 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++
- Print all Semi-Prime Numbers less than or equal to N in C++
- Find all factorial numbers less than or equal to n in C++
- Count Numbers with Unique Digits in C++
- Find maximum product of digits among numbers less than or equal to N in C++
- Print a number strictly less than a given number such that all its digits are distinct in C++
- MySQL query to add 0's to numbers with less than 9 digits?
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 in C++
- Print all Prime Quadruplet of a number less than it in C++
- Count all the numbers less than 10^6 whose minimum prime factor is N C++
- Largest number less than X having at most K set bits in C++
- Numbers At Most N Given Digit Set in C++

Advertisements