This the GFG Link In this link, I am not able to get anything intuition that how we are calculating the number of 2 as a digit in, My doubt is if we are counting the 6000 digits in the range as explained in the below description then why we are simply dividing the number by 10 and returning it, If anyone can help me, please do post your answer with examples
Case digits < 2
Consider the value x = 61523 and digit at index d = 3 (here indexes are considered from right and rightmost index is 0). We observe that x[d] = 1. There are 2s at the 3rd digit in the ranges 2000 – 2999, 12000 – 12999, 22000 – 22999, 32000 32999, 42000 – 42999, and 52000 – 52999. So there are 6000 2’s total in the 3rd digit. This is the same amount as if we were just counting all the 2s in the 3rd digit between 1 and 60000.
In other words, we can round down to the nearest 10d+1, and then divide by 10, to compute the number of 2s in the d-th digit.
if x[d) < 2: count2sinRangeAtDigit(x, d) =
Compute y = round down to nearest 10d+1
return y/10
Case digit > 2
Now, let’s look at the case where d-th digit (from right) of x is greater than 2 (x[d] > 2). We can apply almost the exact same logic to see that there are the same number of 2s in the 3rd digit in the range 0 – 63525 as there as in the range 0 – 70000. So, rather than rounding down, we round up.
if x[d) > 2: count2sinRangeAtDigit(x, d) =
Compute y = round down to nearest 10d+1
return y / 10
Case digit = 2
The final case may be the trickiest, but it follows from the earlier logic. Consider x = 62523 and d = 3. We know that there are the same ranges of 2s from before (that is, the ranges 2000 – 2999, 12000 – 12999, … , 52000 – 52999). How many appear in the 3rd digit in the final, partial range from 62000 – 62523? Well, that should be pretty easy. It’s just 524 (62000, 62001, … , 62523).
if x[d] = 2: count2sinRangeAtDigit(x, d) =
Compute y = round down to nearest 10d+1
Compute z = right side of x (i.e., x% 10d)
return y/10 + z + 1**// here why we are doing it ,what is the logic behind this approach**
There is not complete clarity in the explantion given above that's why I am asking here Thank you
For me that explanation is strange too. Also note that true complexity is O(log(n)) because it depends on nummber length (digit count).
Consider the next example: we have number 6125
.
At the first round we need to calculate how many 2
's are met as the rightmost digit in all numbers from 0
to 6125
. We round number down to 6120
and up to 6130
. Last digit is 5>2
, so we have 613
intervals, every interval contains one digit 2
as the last digit - here we count last 2
's in numbers like 2,12,22,..1352,..,6122
.
At the second round we need to calculate how many 2
's are met as the second (from right) digit in all numbers from 0
to 6125
. We round number down to 6100
and up to 6200
. Also we have right=5
. Digit is 2
, so we have 61 intervals, every interval contains ten digits 2 at the second place (20..29, 120..129... 6020..6029
). We add 61*10
. Also we have to add 5+1
2
's for values 6120..6125
At the third round we need to calculate how many 2
's are met as the third (from right) digit in all numbers from 0
to 6125
. We round number down to 6000
and up to 7000
. Digit is 1
, so we have 6 intervals
, every interval contains one hundred of digit 2 at the third place (200.299.. 5200..5299
). So add 6*100
.
I think it is clear now that we add 1
interval with thousand of 2
's (2000.2999
) as the leftmost digit (6>2
)