Print all numbers less than N with at-most 2 unique digits
Given a number N(less than 10^9). The task is to print all the numbers less than N which are having a maximum of 2 unique digits.
Note: Number such as 100, 111, 101 are valid as the number of unique digits are at most 2 but 123 is invalid as it has 3 unique digits.
Examples:
Input: N = 12
Output: The numbers are: 1 2 3 4 5 6 7 8 9 10 11
Input: N = 154
Output: The numbers are: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 110 111 112 113 114 115 116 117 118 119 121 122 131 133 141 144 151
Approach:
- Since we are said a maximum of two unique digits, two loops will give us all combination of two digits.
- A recursive function is written in terms to generate all the numbers using two digits.
- Call the function with num initially as 0, and generate numbers using recurrence num*10+a and num*10+b, with the base case being num>=n.
- A set can be used to store all the numbers as there can be duplicate elements which the recursion function generates.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
set< int > st;
void generateNumbers( int n, int num, int a, int b)
{
if (num > 0 && num < n)
st.insert(num);
if (num >= n)
return ;
if (num * 10 + a > num)
generateNumbers(n, num * 10 + a, a, b);
generateNumbers(n, num * 10 + b, a, b);
}
void printNumbers( 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: " ;
while (!st.empty()) {
cout << *st.begin() << " " ;
st.erase(st.begin());
}
}
int main()
{
int n = 12;
printNumbers(n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static Set<Integer> st= new HashSet<Integer>();
static void generateNumbers( int n, int num, int a, int b)
{
if (num > 0 && num < n)
st.add(num);
if (num >= n)
return ;
if (num * 10 + a > num)
generateNumbers(n, num * 10 + a, a, b);
generateNumbers(n, num * 10 + b, a, b);
}
static void printNumbers( int n)
{
for ( int i = 0 ; i <= 9 ; i++)
for ( int j = i + 1 ; j <= 9 ; j++)
generateNumbers(n, 0 , i, j);
System.out.print( "The numbers are: " );
System.out.print(st);
st.clear();
}
public static void main(String args[])
{
int n = 12 ;
printNumbers(n);
}
}
|
Python3
st = set ()
def generateNumbers(n, num, a, b):
if (num > 0 and num < n):
st.add(num)
if (num > = n):
return
if (num * 10 + a > num):
generateNumbers(n, num * 10 + a, a, b)
generateNumbers(n, num * 10 + b, a, b)
def printNumbers(n):
for i in range ( 10 ):
for j in range (i + 1 , 10 , 1 ):
generateNumbers(n, 0 , i, j)
print ( "The numbers are:" , end = " " )
l = list (st)
for i in l:
print (i, end = " " )
if __name__ = = '__main__' :
n = 12
printNumbers(n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static SortedSet< int > st = new SortedSet< int >();
static void generateNumbers( int n, int num, int a, int b)
{
if (num > 0 && num < n)
st.Add(num);
if (num >= n)
return ;
if (num * 10 + a > num)
generateNumbers(n, num * 10 + a, a, b);
generateNumbers(n, num * 10 + b, a, b);
}
static void printNumbers( int n)
{
for ( int i = 0; i <= 9; i++)
for ( int j = i + 1; j <= 9; j++)
generateNumbers(n, 0, i, j);
Console.Write( "The numbers are: " );
foreach ( int obj in st)
Console.Write(obj+ " " );
st.Clear();
}
public static void Main(String []args)
{
int n = 12;
printNumbers(n);
}
}
|
Javascript
<script>
let st= new Set();
function generateNumbers(n,num,a,b)
{
if (num > 0 && num < n)
st.add(num);
if (num >= n)
return ;
if (num * 10 + a > num)
generateNumbers(n, num * 10 + a, a, b);
generateNumbers(n, num * 10 + b, a, b);
}
function printNumbers(n)
{
for (let i = 0; i <= 9; i++)
for (let j = i + 1; j <= 9; j++)
generateNumbers(n, 0, i, j);
document.write( "The numbers are: " );
document.write(Array.from(st).sort( function (a,b){ return a-b;}).join( " " ));
st.clear();
}
let n = 12;
printNumbers(n);
</script>
|
Output:
The numbers are: 1 2 3 4 5 6 7 8 9 10 11
Time complexity: O(36* 230)
Space Complexity : O(1)
Last Updated :
13 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...