Given two integer number n and d. The task is to find the number between 0 to n which contain the specific digit d.
Examples:
Input : n = 20
d = 5
Output : 5 15
Input : n = 50
d = 2
Output : 2 12 20 21 22 23 24 25 26 27 28 29 32 42
Approach 1: Take a loop from 0 to n and check each number one by one, if the number contains digit d then print it otherwise increase the number. Continue this process until loop ended.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isDigitPresent( int x, int d)
{
while (x > 0)
{
if (x % 10 == d)
break ;
x = x / 10;
}
return (x > 0);
}
void printNumbers( int n, int d)
{
for ( int i = 0; i <= n; i++)
if (i == d || isDigitPresent(i, d))
cout << i << " " ;
}
int main()
{
int n = 47, d = 7;
printNumbers(n, d);
return 0;
}
|
Java
class GFG
{
static boolean isDigitPresent( int x, int d)
{
while (x > 0 )
{
if (x % 10 == d)
break ;
x = x / 10 ;
}
return (x > 0 );
}
static void printNumbers( int n, int d)
{
for ( int i = 0 ; i <= n; i++)
if (i == d || isDigitPresent(i, d))
System.out.print(i + " " );
}
public static void main(String[] args)
{
int n = 47 , d = 7 ;
printNumbers(n, d);
}
}
|
Python3
def isDigitPresent(x, d):
while (x > 0 ):
if (x % 10 = = d):
break
x = x / 10
return (x > 0 )
def printNumbers(n, d):
for i in range ( 0 , n + 1 ):
if (i = = d or isDigitPresent(i, d)):
print (i,end = " " )
n = 47
d = 7
print ( "The number of values are" )
printNumbers(n, d)
|
C#
using System;
class GFG {
static bool isDigitPresent( int x, int d)
{
while (x > 0)
{
if (x % 10 == d)
break ;
x = x / 10;
}
return (x > 0);
}
static void printNumbers( int n, int d)
{
for ( int i = 0; i <= n; i++)
if (i == d || isDigitPresent(i, d))
Console.Write(i + " " );
}
public static void Main()
{
int n = 47, d = 7;
printNumbers(n, d);
}
}
|
PHP
<?php
function isDigitPresent( $x , $d )
{
while ( $x > 0)
{
if ( $x % 10 == $d )
break ;
$x = $x / 10;
}
return ( $x > 0);
}
function printNumbers( $n , $d )
{
for ( $i = 0; $i <= $n ; $i ++)
if ( $i == $d || isDigitPresent( $i , $d ))
echo $i , " " ;
}
$n = 47;
$d = 7;
printNumbers( $n , $d );
?>
|
Javascript
<script>
function isDigitPresent(x, d)
{
while (x > 0)
{
if (x % 10 == d)
break ;
x = x / 10;
}
return (x > 0);
}
function printNumbers(n, d)
{
for (let i = 0; i <= n; i++)
if (i == d || isDigitPresent(i, d))
document.write(i + " " );
}
let n = 47, d = 7;
printNumbers(n, d);
</script>
|
Complexity Analysis:
- Time Complexity: O(n logn)
- Auxiliary Space: O(1)
Approach 2: This approach uses every number as a String and checks digit is present or not. This approach use of String.indexOf() function to check if the character is present in the string or not.
String.indexOf() >= 0 means character is present and String.indexOf() = -1 means character is not present
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
void printNumbers( int n, int d)
{
string st = "" ;
st += to_string(d);
char ch = st[0];
string p = "" ;
p += ch;
for ( int i = 0; i <= n; i++)
{
st = "" ;
st = st + to_string(i);
int idx = st.find(p);
if (i == d || idx!=-1)
cout << (i) << " " ;
}
}
int main()
{
int n = 100, d = 5;
printNumbers(n, d);
}
|
Java
public class GFG {
static void printNumbers( int n, int d)
{
String st = "" + d;
char ch = st.charAt( 0 );
for ( int i = 0 ; i <= n; i++) {
st = "" ;
st = st + i;
if (i == d || st.indexOf(ch) >= 0 )
System.out.print(i + " " );
}
}
public static void main(String[] args)
{
int n = 100 , d = 5 ;
printNumbers(n, d);
}
}
|
Python3
def index(st, ch):
for i in range ( len (st)):
if (st[i] = = ch):
return i;
return - 1
def printNumbers(n, d):
st = "" + str (d)
ch = st[ 0 ]
for i in range ( 0 , n + 1 , 1 ):
st = ""
st = st + str (i)
if (i = = d or index(st, ch) > = 0 ):
print (i, end = " " )
if __name__ = = '__main__' :
n = 100
d = 5
printNumbers(n, d)
|
C#
using System;
class GFG
{
static void printNumbers( int n, int d)
{
String st = "" + d;
char ch = st[0];
for ( int i = 0; i < n; i++)
{
st = "" ;
st = st + i;
if (i == d || st.IndexOf(ch) >= 0)
Console.Write(i + " " );
}
}
public static void Main()
{
int n = 100, d = 5;
printNumbers(n, d);
}
}
|
Javascript
<script>
function printNumbers(n, d)
{
let st = "" + d;
let ch = st[0];
for (let i = 0; i < n; i++)
{
st = "" ;
st = st + i;
if (i == d || st.indexOf(ch) >= 0)
document.write(i + " " );
}
}
let n = 100, d = 5;
printNumbers(n, d);
</script>
|
Output5 15 25 35 45 50 51 52 53 54 55 56 57 58 59 65 75 85 95
Complexity Analysis:
- Time Complexity: O(n)
- Space Complexity: O(1)