Given a number n and we have to find the n-th number such that its digits only consist of 0, 1, 2, 3, 4, or 5.
Examples :
Input: n = 6
Output: 5
Input: n = 10
Output: 13
We first store 0, 1, 2, 3, 4, 5 in an array. We can see that next numbers will be 10, 11, 12,,13, 14, 15 and after that numbers will be 20, 21, 23, 24, 25 and so on. We can see the pattern that is repeating again and again. We save the calculated result and use it for further calculations.
next 6 numbers are-
1*10+0 = 10
1*10+1 = 11
1*10+2 = 12
1*10+3 = 13
1*10+4 = 14
1*10+5 = 15
and after that next 6 numbers will be-
2*10+0 = 20
2*10+1 = 21
2*10+2 = 22
2*10+3 = 23
2*10+4 = 24
2*10+5 = 25
We use this pattern to find the n-th number. Below is the complete algorithm.
1) push 0 to 5 in ans vector
2) for i=0 to n/6
for j=0 to 6
// this will be the case when first
// digit will be zero
if (ans[i]*10! = 0)
ans.push_back(ans[i]*10 + ans[j])
3) print ans[n-1]
C++
#include <bits/stdc++.h>
using namespace std;
int findNth( int n)
{
vector< int > ans;
for ( int i = 0; i < 6; i++)
ans.push_back(i);
for ( int i = 0; i <= n / 6; i++)
for ( int j = 0; j < 6; j++)
if ((ans[i] * 10) != 0)
ans.push_back(ans[i]
* 10 + ans[j]);
return ans[n - 1];
}
int main()
{
int n = 10;
cout << findNth(n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static int findNth( int n)
{
ArrayList<Integer> ans = new ArrayList<Integer>();
for ( int i = 0 ; i < 6 ; i++)
ans.add(i);
for ( int i = 0 ; i <= n / 6 ; i++)
for ( int j = 0 ; j < 6 ; j++)
if ((ans.get(i) * 10 ) != 0 )
ans.add(ans.get(i) * 10 + ans.get(j));
return ans.get(n - 1 );
}
public static void main(String[] args)
{
int n = 10 ;
int ans = findNth(n);
System.out.println(ans);
}
}
|
Python3
def findNth(n):
ans = []
for i in range ( 6 ):
ans.append(i)
for i in range (n / / 6 + 1 ):
for j in range ( 6 ):
if ((ans[i] * 10 ) ! = 0 ):
ans.append(ans[i]
* 10 + ans[j])
return ans[n - 1 ]
if __name__ = = "__main__" :
n = 10
print (findNth(n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
public static int findNth( int n)
{
List< int > ans = new List< int >();
for ( int i = 0; i < 6; i++)
ans.Add(i);
for ( int i = 0; i <= n / 6; i++)
for ( int j = 0; j < 6; j++)
if ((ans[i] * 10) != 0)
ans.Add(ans[i] * 10 + ans[j]);
return ans[n - 1];
}
public static void Main(String[] args)
{
int n = 10;
int ans = findNth(n);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function findNth(n)
{
var ans = [];
for (i = 0; i < 6; i++)
ans.push(i);
for (i = 0; i <= n / 6; i++)
for (j = 0; j < 6; j++)
if ((ans[i] * 10) != 0)
ans.push(ans[i] * 10 + ans[j]);
return ans[n - 1];
}
var n = 10;
var ans = findNth(n);
document.write(ans);
</script>
|
Time complexity: O(N) where N is given number
Auxiliary space: O(N)
Another Approach:
- Initialize a variable num = 0
- Run a while loop till N != 0.
- Check if num is special
- Decrement N by 1
- Break if N becomes 0
- Increment num by 1
- Return num as the final answer
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
bool isSpecial( int num)
{
while (num != 0) {
int rem = num % 10;
if (rem == 6 || rem == 7 || rem == 8 || rem == 9) {
return false ;
}
num = num / 10;
}
return true ;
}
int getSpecialNumber( int N)
{
int num = 0;
while (N != 0) {
if (isSpecial(num)) {
N--;
}
if (N == 0) {
break ;
}
num++;
}
return num;
}
int main()
{
int n = 10;
cout << getSpecialNumber(n);
return 0;
}
|
Java
import java.io.*;
public class Main {
public static boolean isSpecial( int num) {
while (num != 0 ) {
int rem = num % 10 ;
if (rem == 6 || rem == 7 || rem == 8 || rem == 9 ) {
return false ;
}
num = num / 10 ;
}
return true ;
}
public static int getSpecialNumber( int N) {
int num = 0 ;
while (N != 0 ) {
if (isSpecial(num)) {
N--;
}
if (N == 0 ) {
break ;
}
num++;
}
return num;
}
public static void main(String[] args) {
int n = 10 ;
System.out.println(getSpecialNumber(n));
}
}
|
Python3
def isSpecial(num):
while num ! = 0 :
rem = num % 10
if rem = = 6 or rem = = 7 or rem = = 8 or rem = = 9 :
return False
num = num / / 10
return True
def getSpecialNumber(N):
num = 0
while N ! = 0 :
if isSpecial(num):
N - = 1
if N = = 0 :
break
num + = 1
return num
n = 10
print (getSpecialNumber(n))
|
C#
using System;
class GFG{
static bool IsSpecial( int num)
{
while (num != 0) {
int rem = num % 10;
if (rem == 6 || rem == 7 || rem == 8
|| rem == 9) {
return false ;
}
num = num / 10;
}
return true ;
}
static int GetSpecialNumber( int N)
{
int num = 0;
while (N != 0) {
if (IsSpecial(num)) {
N--;
}
if (N == 0) {
break ;
}
num++;
}
return num;
}
static void Main( string [] args)
{
int n = 10;
Console.WriteLine(GetSpecialNumber(n));
}
}
|
Javascript
function isSpecial(num)
{
while (num != 0) {
let rem = num % 10;
if (rem == 6 || rem == 7 || rem == 8 || rem == 9) {
return false ;
}
num = num / 10;
}
return true ;
}
function getSpecialNumber(N)
{
let num = 0;
while (N != 0) {
if (isSpecial(num)) {
N--;
}
if (N == 0) {
break ;
}
num++;
}
return num;
}
let n = 10;
console.log(getSpecialNumber(n));
|
Time Complexity: O(n log n)
Space Complexity: O(1)
Efficient Method :
Algorithm :
- First, convert number n to base 6.
- Store the converted value simultaneously in an array.
- Print that array in reverse order.
Below is the implementation of the above algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
#define max 100000
int baseconversion( int arr[], int num, int base)
{
int i = 0, rem, j;
if (num == 0) {
return 0;
}
while (num > 0) {
rem = num % base;
arr[i++] = rem;
num /= base;
}
return i;
}
int main()
{
int arr[max] = { 0 };
int n = 10;
int size = baseconversion(arr, n - 1, 6);
if (size == 0)
cout << size;
for ( int i = size - 1; i >= 0; i--) {
cout << arr[i];
}
return 0;
}
|
Java
class GFG {
static final int max = 100000 ;
static int baseconversion( int arr[],
int num, int base)
{
int i = 0 , rem, j;
if (num == 0 ) {
return 0 ;
}
while (num > 0 ) {
rem = num % base;
arr[i++] = rem;
num /= base;
}
return i;
}
public static void main (String[] args)
{
int arr[] = new int [max];
int n = 10 ;
int size = baseconversion(arr, n - 1 , 6 );
if (size == 0 )
System.out.print(size);
for ( int i = size - 1 ; i >= 0 ; i--) {
System.out.print(arr[i]);
}
}
}
|
Python3
max = 100000 ;
def baseconversion(arr, num, base):
i = 0 ;
if (num = = 0 ):
return 0 ;
while (num > 0 ):
rem = num % base;
i = i + 1 ;
arr[i] = rem;
num = num / / base;
return i;
if __name__ = = '__main__' :
arr = [ 0 for i in range ( max )];
n = 10 ;
size = baseconversion(arr, n - 1 , 6 );
if (size = = 0 ):
print (size);
for i in range (size, 0 , - 1 ):
print (arr[i], end = "");
|
C#
using System;
class GFG {
static int max = 100000;
static int baseconversion( int []arr,
int num, int bas)
{
int i = 0, rem;
if (num == 0) {
return 0;
}
while (num > 0) {
rem = num % bas;
arr[i++] = rem;
num /= bas;
}
return i;
}
public static void Main ()
{
int []arr = new int [max];
int n = 10;
int size = baseconversion(arr, n - 1, 6);
if (size == 0)
Console.Write(size);
for ( int i = size - 1; i >= 0; i--) {
Console.Write(arr[i]);
}
}
}
|
Javascript
<script>
var max = 100000;
function baseconversion(arr , num , base) {
var i = 0, rem, j;
if (num == 0) {
return 0;
}
while (num > 0) {
rem = num % base;
arr[i++] = rem;
num = parseInt(num/base);
}
return i;
}
var arr = Array(max).fill(0);
var n = 10;
var size = baseconversion(arr, n - 1, 6);
if (size == 0)
document.write(size);
for (i = size - 1; i >= 0; i--) {
document.write(arr[i]);
}
</script>
|
Output:
13
Time Complexity: O(log(n))
Space Complexity: O(log(n))
Another Efficient Method:
Algorithm:
- Decrease the number N by 1.
- Convert the number N to base 6.
Below is the implementation of the above algorithm :
C++
#include <iostream>
using namespace std;
int ans( int n){
if (n < 6){
return n;
}
return n%6 + 10*(ans(n/6));
}
int getSpecialNumber( int N)
{
return ans(--N);
}
int main()
{
int N = 17;
int answer = getSpecialNumber(N);
cout<<answer<<endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int ans( int n)
{
if (n < 6 )
{
return n;
}
return n % 6 + 10 * (ans(n / 6 ));
}
static int getSpecialNumber( int N)
{
return ans(--N);
}
public static void main(String[] args)
{
int N = 17 ;
int answer = getSpecialNumber(N);
System.out.println(answer);
}
}
|
Python3
def ans(n):
if (n < 6 ):
return n
return n % 6 + 10 * (ans(n / / 6 )) - 1
def getSpecialNumber(N):
return ans(N)
if __name__ = = '__main__' :
N = 17
answer = getSpecialNumber(N)
print (answer)
|
C#
using System;
public class GFG{
static int ans( int n)
{
if (n < 6)
{
return n;
}
return n % 6 + 10 * (ans(n / 6));
}
static int getSpecialNumber( int N)
{
return ans(--N);
}
public static void Main(String[] args)
{
int N = 17;
int answer = getSpecialNumber(N);
Console.WriteLine(answer);
}
}
|
Javascript
<script>
function ans(n) {
if (n < 6) {
return n;
}
return n % 6 + 10 * (ans(parseInt(n / 6)));
}
function getSpecialNumber(N) {
return ans(--N);
}
var N = 17;
var answer = getSpecialNumber(N);
document.write(answer);
</script>
|
Time Complexity: O(logN)
Auxiliary Space: O(1)