# Find smallest perfect square number A such that N + A is also a perfect square number

Given a positive number N. The task is to find out the smallest perfect square number A such that N + A is also a perfect square number or return -1.

Examples:

```Input: N = 3
Output: 1
Explanation:
As 1 + 3 = 4 = 22

Input: N=1
Output: -1
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach:
Traverse M from {1, 2, 3, 4, 5…} and check whether (N + M * M) is a perfect square number or not.

Efficient Approach:

• On observing, we have an equation like:

• N + (X * X) = (M * M) where N is given and M and X are unknown.
• We can rearrange it and get:
• N = (M * M) – (X * X)
• N = (M + X) * (M – X)
• Now we can see that for obtaining N, we need to find the factor of N.The factor of N can be obtained in O(N) time. But it can be optimized to O(N^1/2) by this method.
• Let the factor of N be a and b = (N / a). So, from the above equation a = (M – X) and b = (M + X), and after solving this we can obtain the value of X = (b – a)/2.

Below is the implementation of the above approach:

## C++

 `// C++ code to find out the smallest  ` `// perfect square X which when added to N  ` `// yields another perfect square number.  ` `#include ` `using` `namespace` `std; ` ` `  `long` `SmallestPerfectSquare(``long` `N){ ` `     `  `    ``// X is the smallest perfect  ` `    ``// square number  ` `    ``long` `X = (``long``)1e9;  ` `    ``long` `ans;  ` `     `  `    ``// Loop from 1 to square root of N  ` `    ``for``(``int` `i = 1; i < ``sqrt``(N); i++) ` `    ``{  ` `     `  `        ``// Condition to check whether i  ` `        ``// is factor of N or not  ` `        ``if` `(N % i == 0) ` `        ``{  ` `            ``long` `a = i;  ` `            ``long` `b = N / i;  ` `     `  `            ``// Condition to check whether  ` `            ``// factors satisfies the  ` `            ``// equation or not  ` `            ``if``((b - a != 0) && ((b - a) % 2 == 0)) ` `            ``{  ` `                         `  `                ``// Stores minimum value  ` `                ``X = min(X, (b - a) / 2);  ` `            ``}  ` `        ``}  ` `    ``}  ` `     `  `    ``// Return if X * X if X is not equal  ` `    ``// to 1e9 else return -1  ` `    ``if` `(X != 1e9)  ` `        ``ans = X * X;  ` `    ``else` `        ``ans = -1;  ` `             `  `    ``return` `ans;  ` `}  ` `     `  `// Driver code  ` `int` `main()  ` `{ ` `    ``long` `N = 3;  ` `    ``cout << SmallestPerfectSquare(N);  ` `    ``return` `0; ` `}  ` ` `  `// This code is contributed by AnkitRai01  `

## Java

 `// Java code to find out the smallest  ` `// perfect square X which when added to N  ` `// yields another perfect square number.  ` ` `  `public` `class` `GFG { ` `     `  ` `  `    ``static` `long` `SmallestPerfectSquare(``long` `N) ` `    ``{ ` `     `  `        ``// X is the smallest perfect  ` `        ``// square number  ` `        ``long` `X = (``long``)1e9; ` `        ``long` `ans; ` `     `  `        ``// Loop from 1 to square root of N  ` `        ``for``(``int` `i = ``1``; i < Math.sqrt(N); i++){  ` `     `  `            ``// Condition to check whether i  ` `            ``// is factor of N or not  ` `            ``if` `(N % i == ``0``){ ` `                ``long` `a = i ; ` `                ``long` `b = N / i;  ` `     `  `                ``// Condition to check whether  ` `                ``// factors satisfies the  ` `                ``// equation or not  ` `                ``if` `((b - a != ``0``) && ((b - a) % ``2` `== ``0``)){ ` `                         `  `                    ``// Stores minimum value  ` `                    ``X = Math.min(X, (b - a) / ``2``) ; ` `                ``} ` `            ``} ` `        ``} ` `     `  `        ``// Return if X * X if X is not equal  ` `        ``// to 1e9 else return -1  ` `        ``if` `(X != 1e9) ` `            ``ans = X * X; ` `        ``else` `            ``ans = -``1``; ` `             `  `        ``return` `ans;  ` `    ``} ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args){ ` `        ``long` `N = ``3``; ` `         `  `        ``System.out.println(SmallestPerfectSquare(N)) ; ` `     `  `        ``} ` `} ` `// This code is contributed by AnkitRai01 `

## Python3

 `# Python3 code to find out the smallest ` `# perfect square X which when added to N ` `# yields another perfect square number. ` `import` `math ` `def` `SmallestPerfectSquare(N): ` ` `  `    ``# X is the smallest perfect  ` `    ``# square number  ` `    ``X ``=` `1e9` ` `  `    ``# Loop from 1 to square root of N ` `    ``for` `i ``in` `range``(``1``, ``int``(math.sqrt(N)) ``+` `1``): ` ` `  `        ``# Condition to check whether i  ` `        ``# is factor of N or not ` `        ``if` `N ``%` `i ``=``=` `0``: ` `            ``a ``=` `i ` `            ``b ``=` `N ``/``/` `i   ` ` `  `            ``# Condition to check whether   ` `            ``# factors satisfies the  ` `            ``# equation or not  ` `            ``if` `b ``-` `a !``=` `0` `and` `(b ``-` `a) ``%` `2` `=``=` `0``: ` `                    `  `                ``# Stores minimum value ` `                 ``X ``=` `min``(X, (b ``-` `a) ``/``/` `2``) ` ` `  `    ``# Return if X * X if X is not equal ` `    ``# to 1e9 else return -1 ` `    ``return``(X ``*` `X ``if` `X !``=` `1e9` `else` `-``1``) ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:   ` `   `  `    ``N ``=` `3` `   `  `    ``print``(SmallestPerfectSquare(N)) `

## C#

 `// C# code to find out the smallest  ` `// perfect square X which when added to N  ` `// yields another perfect square number.  ` `using` `System; ` ` `  `class` `GFG {  ` `     `  `    ``static` `long` `SmallestPerfectSquare(``long` `N)  ` `    ``{  ` `        ``// X is the smallest perfect  ` `        ``// square number  ` `        ``long` `X = (``long``)1e9;  ` `        ``long` `ans;  ` `     `  `        ``// Loop from 1 to square root of N  ` `        ``for``(``int` `i = 1; i < Math.Sqrt(N); i++){  ` `     `  `            ``// Condition to check whether i  ` `            ``// is factor of N or not  ` `            ``if` `(N % i == 0) ` `            ``{  ` `                ``long` `a = i;  ` `                ``long` `b = N / i;  ` `     `  `                ``// Condition to check whether  ` `                ``// factors satisfies the  ` `                ``// equation or not  ` `                ``if` `((b - a != 0) && ((b - a) % 2 == 0)) ` `                ``{  ` `                    ``// Stores minimum value  ` `                    ``X = Math.Min(X, (b - a) / 2);  ` `                ``}  ` `            ``}  ` `        ``}  ` `     `  `        ``// Return if X*X if X is not equal  ` `        ``// to 1e9 else return -1  ` `        ``if` `(X != 1e9)  ` `            ``ans = X * X;  ` `        ``else` `            ``ans = -1;  ` `             `  `        ``return` `ans;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main (``string``[] args) ` `    ``{  ` `        ``long` `N = 3;  ` `        ``Console.WriteLine(SmallestPerfectSquare(N));  ` `    ``}  ` `}  ` ` `  `// This code is contributed by AnkitRai01  ` ` `

Output:

```1
```

Time complexity: sqrt(N) My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : AnkitRai01

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.