most competitors use this approach in this problem
check max between wight between low and high
so res=max(wight(low),wight(high).
then putting current =5
then loop while current<=high
get max between res and and wight(current) and wight(current-1)
then current=current*10
I know this algorithm work correctly but what's the proof
for example if the range 1-783432
so the result max wight between (783432,500000,499999)
It's hard to give such an advise as I don't know your math level.
Let us examine expression for x:
x * (max-x) = max*x - x^2
and the same expression for (x+1):
(x+1) * (max-x-1) = max*x - x^2 + max - 2*x - 1
Difference of these expressions is:
max - 2*x -1
It is clear that difference is positive (so the expression is growing) while max is greater than 2*x, i.e. while x is less than max/2.
If it is growing to max/2 and then decreasing - it is clear that at x=max/2 there is best value.
(surely it is easier to prove when you know the fact which you want to prove)
Let's draw a graph of the width function. As we discussed each part of the graph is a parabola (parabola is a graph of quadratic equation).
Let's find minimum of function (99...9 - x) * x. Roots are 0 and 99...9. As we know extremum of parabola is exactly at the middle of its roots.