# Help me in solving MELTGOLD problem

### My issue

Verdict: time limit exceeded. Output is correct but I have to optimize the code that TLE is avoided.

### My code

# cook your dish here
t = int(input())
for i in range(t):
x, y = map(int, input().split())
time = 0
# x %= pow(10, 9) + 7
# y %= pow(10, 9) + 7

if x == y:
print(0)

else:
while(x>y):
time += 1
y += time

print(time)


Problem Link: MELTGOLD Problem - CodeChef

The simulation approach will result in a TLE, as you said. However there is a math based solution you can use. Each minute, we increase the temperature by i. The first minute, we increase the temperature by 1. Then 2, 3, 4, and so on. If we write these increments as a sum, we get 1 + 2 + 3 + \dots + n - 1 + n (where n is the last minute). The sum can be written as \frac{n(n + 1)}{2}. This sum equals X - Y (the difference between the melting and current temperatures). Its easy to see now that the answer is simply n. Solving for n yields n(n + 1) = 2 \cdot (X - Y). However this calculation becomes tricky. To simplify it, we can estimate n(n + 1) as n^2. Then we get n^2 \approx 2 \cdot (X - Y) \Rightarrow n \approx \sqrt{2 \cdot (X - Y)}. We will then have to \mathbf{round}(\sqrt{2 \cdot (X - Y)}) to account for the simplification we did earlier.

Got it! Thanks for the help!