# n-th number with digits in {0, 1, 2, 3, 4, 5}

Given a number n and we have to find n-th number such that it’s digits only consist 0, 1, 2, 3, 4 or 5.

Examples :

```Input  : n = 6
Output : 5

Input :  n = 10
Output : 13
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

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 complete algorithm.

```1) push 0 to 5 in ans vector
2) for i=0 to n
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++ program to find n-th number with digits ` `// in {0, 1, 2, 3, 4, 5} ` `#include ` `using` `namespace` `std; ` ` `  `// Returns the N-th number with given digits ` `int` `findNth(``int` `n) ` `{ ` `    ``// vector to store results ` `    ``vector<``int``> ans; ` ` `  `    ``// push first 6 numbers in the answer ` `    ``for` `(``int` `i = 0; i < 6; i++) ` `        ``ans.push_back(i); ` ` `  `    ``// calculate further results ` `    ``for` `(``int` `i = 0; i <= n; 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]; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 10; ` `    ``cout << findNth(n); ` `    ``return` `0; ` `} `

Efficient Method :

Algorithm :
1. First convert number n to base 6.
2. Store the converted value simultaneously in an array.
3. Print that array in reverse order.

Below is the implementation of above algorithm :

## C++

 `// CPP code to find nth number ` `// with digits 0, 1, 2, 3, 4, 5 ` `#include ` `using` `namespace` `std; ` ` `  `#define max 100000 ` ` `  `// function to convert num to base 6 ` `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; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``// initialize an array to 0 ` `    ``int` `arr[max] = { 0 }; ` ` `  `    ``int` `n = 10; ` ` `  `    ``// function calling to convert ` `    ``// number n to base 6 ` `    ``int` `size = baseconversion(arr, n - 1, 6); ` ` `  `    ``// if size is zero then return zero ` `    ``if` `(size == 0) ` ` `  `        ``cout << size; ` ` `  `    ``for` `(``int` `i = size - 1; i >= 0; i--) { ` ` `  `        ``cout << arr[i]; ` `    ``} ` ` `  `    ``return` `0; ` `} ` ` `  `// Code is contributed by Anivesh Tiwari. `

## Java

 `// Java code to find nth number ` `// with digits 0, 1, 2, 3, 4, 5 ` `class` `GFG { ` `     `  `    ``static` `final` `int` `max = ``100000``; ` `     `  `    ``// function to convert num to base 6 ` `    ``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; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `         `  `        ``// initialize an array to 0 ` `        ``int` `arr[] = ``new` `int``[max]; ` `     `  `        ``int` `n = ``10``; ` `     `  `        ``// function calling to convert ` `        ``// number n to base 6 ` `        ``int` `size = baseconversion(arr, n - ``1``, ``6``); ` `     `  `        ``// if size is zero then return zero ` `        ``if` `(size == ``0``) ` `            ``System.out.print(size); ` `     `  `        ``for` `(``int` `i = size - ``1``; i >= ``0``; i--) { ` `            ``System.out.print(arr[i]); ` `        ``} ` `    ``} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## C#

 `// C# code to find nth number ` `// with digits 0, 1, 2, 3, 4, 5 ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``static` `int` `max = 100000; ` `     `  `    ``// function to convert num to base 6 ` `    ``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; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main () ` `    ``{ ` `        ``// initialize an array to 0 ` `        ``int` `[]arr = ``new` `int``[max]; ` `     `  `        ``int` `n = 10; ` `     `  `        ``// function calling to convert ` `        ``// number n to base 6 ` `        ``int` `size = baseconversion(arr, n - 1, 6); ` `     `  `        ``// if size is zero then return zero ` `        ``if` `(size == 0) ` `            ``Console.Write(size); ` `     `  `        ``for` `(``int` `i = size - 1; i >= 0; i--) { ` `            ``Console.Write(arr[i]); ` `        ``} ` `    ``} ` `} ` ` `  `// This code is contributed by nitin mittal `

Output:

```13
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Improved By : nitin mittal