Pythagoras Triplet challenge

Question - https://hack.codingblocks.com/contests/c/537/915
My solution - https://ide.codingblocks.com/s/37686

Time limit exceeded for last two cases . First three cases passed . How can I optimize it and make it more efficient ?

There is no need to use loops in this ques… It can be solved using a direct formula… Now you just have to figure out that formula…

https://ide.codingblocks.com/s/40770
3 test cases are failing

Hey Akshay, the formula you are using is not correct, there is no need to calculate the sqrt.
If n is odd,
a = ((n^2)-1)/2
b = ((n^2)+1)/2

if n is even
a = ((n^2)/4)-1
b = ((n^2)/4)+1

If the input number occurs in two or more Pythagorean triplets, from which triplet does one print the output?
For example, if n=5;
should we print 3 and 4 or 12 and 13 ? Why so ?
SInce (3,4,5) is a Pythagorean Triplet, and so is (5,12,13)

1 Like

Hey @S18LPC-OL0076
It is clearly mentioned in the question
Given n , print its Pythagoras pair “”“in increasing order”""" if they exist.

Hence for 5, you have to give 12 and 13.

Use the formula as mentioned by @sanjeetboora
It would give you the required results as per the question.

can you tel me how did you got this formula?