You have given two number n and k. You need to find the n-th number that contains the digit k or divisible by k (2 <= k <=9 ).
Examples:
Input : n = 15, k = 3
Output : 33
Explanation : ( 3, 6, 9, 12, 13, 15, 18, 21, 23, 24,
27, 30, 31, 33 ). These are those number who contain
the digit k = 3 or divisible by k and in this nth number
is 33. so output is 33.
Input : n = 10, k = 2
Output : 20
Explanation : ( 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 )
These are those number who contain the digit k = 2 or
divisible by k and in this nth number is 20. so output
is 20.
Approach:
Check every number from k who contains the digit k or divisible by k until we did not get nth number.
C++
#include <bits/stdc++.h>
using namespace std;
int checkdigit(int n, int k)
{
while (n)
{
int rem = n % 10;
if (rem == k)
return 1;
n = n / 10;
}
return 0;
}
int findNthNumber(int n, int k)
{
for (int i = k + 1, count = 1; count < n; i++)
{
if (checkdigit(i, k) || (i % k == 0))
count++;
if (count == n)
return i;
}
return -1;
}
int main()
{
int n = 10, k = 2;
cout << findNthNumber(n, k) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
public static boolean checkdigit(int n, int k)
{
while (n != 0)
{
int rem = n % 10;
if (rem == k)
return true;
n = n / 10;
}
return false;
}
public static int findNthNumber(int n, int k)
{
for (int i = k + 1, count = 1; count < n; i++)
{
if (checkdigit(i, k) || (i % k == 0))
count++;
if (count == n)
return i;
}
return -1;
}
public static void main (String[] args)
{
int n = 10, k = 2;
System.out.println(findNthNumber(n, k));
}
}
|
Python3
def checkdigit(n, k):
while (n):
rem = n % 10
if (rem == k):
return 1
n = n / 10
return 0
def findNthNumber(n, k):
i = k + 1
count = 1
while(count < n):
if (checkdigit(i, k) or (i % k == 0)):
count += 1
if (count == n):
return i
i += 1
return -1
n = 10
k = 2
print(findNthNumber(n, k))
|
C#
using System;
class GFG
{
public static bool checkdigit(int n, int k)
{
while (n != 0)
{
int rem = n % 10;
if (rem == k)
return true;
n = n / 10;
}
return false;
}
public static int findNthNumber(int n, int k)
{
for (int i = k + 1, count = 1; count < n; i++)
{
if (checkdigit(i, k) || (i % k == 0))
count++;
if (count == n)
return i;
}
return -1;
}
public static void Main ()
{
int n = 10, k = 2;
Console.WriteLine(findNthNumber(n, k));
}
}
|
PHP
<?php
function checkdigit($n, $k)
{
while ($n)
{
$rem = $n % 10;
if ($rem == $k)
return 1;
$n = $n / 10;
}
return 0;
}
function findNthNumber($n, $k)
{
for ($i = $k + 1, $count = 1;
$count < $n; $i++)
{
if (checkdigit($i, $k) ||
($i % $k == 0))
$count++;
if ($count == $n)
return $i;
}
return -1;
}
$n = 10; $k = 2;
echo findNthNumber($n, $k);
?>
|
Javascript
<script>
function checkdigit(n, k)
{
while (n != 0)
{
let rem = n % 10;
if (rem == k)
return true;
n = n / 10;
}
return false;
}
function findNthNumber(n, k)
{
for (let i = k + 1, count = 1; count < n; i++)
{
if (checkdigit(i, k) || (i % k == 0))
count++;
if (count == n)
return i;
}
return -1;
}
let n = 10, k = 2;
document.write(findNthNumber(n, k));
</script>
|
Output:
20
Time Complexity: O(nlog(n)), since this is a brute force approach.
Space Complexity: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!