WA in Chef and Squares

https://www.codechef.com/viewsolution/32703513
This is my link to the solution,please tell me what testcase i may be wrong?

For n=9 , it should be 0, you are printing 16. You are just missing the case when n is perfect square, you have to print 0.

1 is also a perfect square , the example gives -1 , i have realized my mistake . I am getting x, perfect square but not the smallest.

Then for n=4, it should be -1

ya i think so, done btw CodeChef: Practical coding for everyone

You have changed the logic.
Which tc you were missing?

The solution I did first would give me a perfect square which on added to this number gives me a perfect square. But i didn’t knew that this number would be the smallest perfect square.

let’s take the number 15 , according to my first solution , i would get the answer as 49.
But if i added 1 to this number I will get 16 which is a perfect square i got on adding 1(a perfect square).
So the answer should be 1, but i got 49.
The problem is related to factors.

let the given number be n.
we have to take a number i as a perfect square and on addition it should give a square of a number j.

n+ii=jj;

Therefore, n=(j-i)*(j+i);

Now we have to check for the factors and minimize the value of j.

J would be minimized if we take the factors which are closer to the square root of n.

for example , we take the number 15.

15=1*(15) ,15=(3)*(5);
We will take the second prime factors ,so we get the value of j as 4.

1 Like