Find the number of 2's between 0 and n

158 Views Asked by At

Find the number of 2's between 0 and n?

The way I thought is to count how many 2's are there of length k(number of digits in n).

This is similar to find all permutation of digit[0-9] of length k that have at least one 2 in it.

Is there a better way to do this?