Detailed Explanation:
Convert N->N-1
to exclude 0
.
Let A = {0, 2, 4, 6, 8}
Let's count how many numbers have k
, (k > 1)
digits and all their digits belong to set {0, 2, 4, 6, 8}
. First digits can be chosen in 4
ways, all other digits in 5
ways, so in total, 4*pow(5, k-1)
. Which means, if m = ceil(log5(N))
, then N
th number in series has m digits.
Now, if the first digit in base-5
representation of N
equals i
, (1 <= i <= 4)
then first digit in our answer is A[i]
. Same holds second digit and so on.
So in your algorithms you need to present N
in base-5
, e.g. N=abcde..
, where each of a,b,c
is in range [0..4]
and ur answer will be a result of mapping each of a
-> A[a]