I tested the problem with the code that you gave:
the only thing, the one that gives you wrong answer, is that you are not uptadating the maximum length to zero again... look try with this example.
100 200 = 125 //this is the correct answer
201 210 = 89 //same
but your program keeps giving 125 ...