Wrong answer of Pythagoras Triplet

Please help me find the error in my code. Had it been giving TLE, it would have been still fine, as my approach is in O(n) complexity. But why is it giving wrong answer?

Hi Rishabh,
Can you please share your code/approach with me?

I take a number, then do linear search for a number such that it becomes the hypotenuse of a triangle with one side as first number. if sqrt of (hypotenuse-side1) is an integer, then that hypotenuse is correct.

can you share your code?

Hi Rishabh,
First of all, you are taking t = 10^9 which is wrong because, the question says n<=10^9. Which means, the answer can exceed 10^9. Also you need to put a check condition for n=1 or n=2 because no triplets are possible for them. And when we are dealing with such big numbers we usually avoid the O(n) approach, instead try to simplify the equation and work out the answer. a^2 + b^2 = c^2. Hence we manipulate the equation as a^2 = (c+b)(c-b). Now if the given number is even i.e. a is even, we can express it as product of 2 even numbers say 2 and (a^2)/2.

(a^2)/2 * 2 = (c-b)(c+b)
hence c+b = 2 and c-b = (a^2)/2. Now solve for b and c and do similarly if given n is odd. (NOTE: I have taken a = n)

If the provided input is 60, we can have answer 63 87, but we can also have 11,61. Now how do I know, which one is acceptable in test case?

If multiple answers are possible, you can print any. But you need to correct your code first.