count
to 0, which will hold the total count of digit '1'.i
from 1 and keep multiplying it by 10 in each iteration until i
is less than or equal to n
.i
, calculate the number of full sets of '1's as (n / (i * 10)) * i
.min(max((n % (i * 10)) - i + 1, 0), i)
.count
.count
after the loop ends.