Pythagoras triplet doubt , can't get all 5 test cases correct

I am not able to detect the error in my code ,
Code link - https://ide.codingblocks.com/s/112166
When submitted , it shows 3 successful test cases out of 5 , but i keep getting wrong answer in other 2.
course - c++ launchpad
challenge fundamental.

@vyomgarg47
There is no need to use loops for this problem .
Refer to this hint video. I’m sure it will help you a lot.
https://youtu.be/PUb1_R3p41c

Yeah , I watched the video after this attempt on the problem , but I was wondering what’s wrong in my code…If this could also be accepted as a valid solution ? It does print the pythagorean triplets , and also print -1 when there aren’t any. Though , it’s not the best piece of code , but still…

@vyomgarg47
There are a lot of numbers which are present in more than one pythagorean triplet.
Consider these triplets :
7,24,25
10,24,26
18,24,30
24,143,145

24 is present in 4 of them. There might even be more for just 24 and a lot of other numbers are present in multiple such triplets.
While your code generates one of them , the formula gives another. Hence you get the wrong answer.
Also since your code uses loops , your time complexity increases drastically. It is mentioned that the input value will be as large as 10^9.
If you even use a O(n) method , even that will give you a TLE, if not wrong answer.
And your code takes approxiamately O(n log n) time due to sqrt function in each iteration.
This is why I suggest you to use the method discussed in the video as it does this work in O(1) complexity which is the absolute best.

The method mentioned in the video was great , I just wanted to first give it a try myself… I did get TLE on the remaining 2 cases I was taking about , but I was only able to change them to wrong answer :joy:. However , I am new to this programming culture , hence the time complexity stuff went over my head , hope I would be able to understand them in the future… Still , thanks for your help. Really appreciate it.

@vyomgarg47
I would recommend you to try some basic questions on time complexity analysis of codes. It’s pretty simple and you’ll be able to understand it rather quick. Its really not difficult. All you need to know is that the standard taken for a computer is that it can perform 10^8 instructions in a second and this is something followed generally by all online judges. So if you find your code requires performs more instructions than that to solve the problem , it will give a TLE.