What is wrong with this approach in solving problem JADEJA?

I read this problem and concluded that we have to count the number points with integral coordinates(both coordinates). So with two given points I got the equation of the line and then by looping from minimum of x1,x2 to maximum of x1,x2 (excluding both), I am calculating the value of y (by using the above equation of line). And hence the number of “integral” y coordinates will be the answer.
Fortunately I am getting wrong answer.
Please rectify any mistakes in this approach and please don’t suggest any new approach.

Your method is correct, in that it should give you the right answer, and a wrong answer will only occur in case of some fault in your code (precision errors in case you’re using floats etc.).

However, your method is bound to give you a time limit exceeded because your program loops from a to b where a can be as small as 10^-9 and b can be as large as 10^9. Therefore, you will be checking the y value for 2*10^9 points which will definitely time out.

1 Like

Okay, first I’ll try to rectify my mistake.
And once I get TLE, I will try to think of a better approach…