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
Naive Approach:
Traverse M from {1, 2, 3, 4, 5…} and check whether (N + M * M) is a perfect square number or not.
C++
#include<bits/stdc++.h>
using namespace std;
bool checkperfectsquare( int n)
{
if ( ceil (( double ) sqrt (n)) == floor (( double ) sqrt (n))) {
return true ;
}
else {
return false ;
}
}
long SmallestPerfectSquare( long N){
long X = ( long )1e9;
long ans=-1;
for ( int i=1;i<=X;i++)
{
if (checkperfectsquare(N+i*i))
{
ans=i*i;
break ;
}
}
return ans;
}
int main()
{
long N = 3;
cout << SmallestPerfectSquare(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static boolean checkperfectsquare( long n)
{
if (Math.ceil(Math.sqrt(n))
== Math.floor(Math.sqrt(n))) {
return true ;
}
else {
return false ;
}
}
public static long SmallestPerfectSquare( long N)
{
long X = ( long )1e9;
long ans = - 1 ;
for ( int i = 1 ; i <= X; i++) {
if (checkperfectsquare(N + ( long )i * i)) {
ans = i * i;
break ;
}
}
return ans;
}
public static void main(String[] args)
{
long N = 3 ;
System.out.println(SmallestPerfectSquare(N));
}
}
|
Python3
import math
def checkperfectsquare(n):
if math.ceil(math.sqrt(n)) = = math.floor(math.sqrt(n)):
return True
else :
return False
def SmallestPerfectSquare(N):
X = int ( 1e9 )
ans = - 1
for i in range ( 1 , X + 1 ):
if checkperfectsquare(N + i * i):
ans = i * i
break
return ans
N = 3
print (SmallestPerfectSquare(N))
|
C#
using System;
class Program
{
static bool CheckPerfectSquare( long n)
{
if (Math.Ceiling(Math.Sqrt(n)) == Math.Floor(Math.Sqrt(n)))
{
return true ;
}
else
{
return false ;
}
}
static long SmallestPerfectSquare( long N)
{
long X = ( long )1e9;
long ans = -1;
for ( int i = 1; i <= X; i++)
{
if (CheckPerfectSquare(N + i * i))
{
ans = i * i;
break ;
}
}
return ans;
}
static void Main()
{
long N = 3;
Console.WriteLine(SmallestPerfectSquare(N));
}
}
|
Javascript
function checkperfectsquare(n) {
if (Math.ceil(Math.sqrt(n)) == Math.floor(Math.sqrt(n))) {
return true ;
} else {
return false ;
}
}
function SmallestPerfectSquare(N) {
const X = 1000000000;
let ans = -1;
for (let i = 1; i <= X; i++) {
if (checkperfectsquare(N + i * i)) {
ans = i * i;
break ;
}
}
return ans;
}
const N = 3;
console.log(SmallestPerfectSquare(N));
|
Time complexity: O(X*log(X)) where X=1e9 here
Auxiliary Space: O(1)
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++
#include<bits/stdc++.h>
using namespace std;
long SmallestPerfectSquare( long N){
long X = ( long )1e9;
long ans;
for ( int i = 1; i < sqrt (N); i++)
{
if (N % i == 0)
{
long a = i;
long b = N / i;
if ((b - a != 0) && ((b - a) % 2 == 0))
{
X = min(X, (b - a) / 2);
}
}
}
if (X != 1e9)
ans = X * X;
else
ans = -1;
return ans;
}
int main()
{
long N = 3;
cout << SmallestPerfectSquare(N);
return 0;
}
|
Java
public class GFG {
static long SmallestPerfectSquare( long N)
{
long X = ( long )1e9;
long ans;
for ( int i = 1 ; i < Math.sqrt(N); i++){
if (N % i == 0 ){
long a = i ;
long b = N / i;
if ((b - a != 0 ) && ((b - a) % 2 == 0 )){
X = Math.min(X, (b - a) / 2 ) ;
}
}
}
if (X != 1e9)
ans = X * X;
else
ans = - 1 ;
return ans;
}
public static void main (String[] args){
long N = 3 ;
System.out.println(SmallestPerfectSquare(N)) ;
}
}
|
Python3
import math
def SmallestPerfectSquare(N):
X = 1e9
for i in range ( 1 , int (math.sqrt(N)) + 1 ):
if N % i = = 0 :
a = i
b = N / / i
if b - a ! = 0 and (b - a) % 2 = = 0 :
X = min (X, (b - a) / / 2 )
return (X * X if X ! = 1e9 else - 1 )
if __name__ = = "__main__" :
N = 3
print (SmallestPerfectSquare(N))
|
C#
using System;
class GFG {
static long SmallestPerfectSquare( long N)
{
long X = ( long )1e9;
long ans;
for ( int i = 1; i < Math.Sqrt(N); i++){
if (N % i == 0)
{
long a = i;
long b = N / i;
if ((b - a != 0) && ((b - a) % 2 == 0))
{
X = Math.Min(X, (b - a) / 2);
}
}
}
if (X != 1e9)
ans = X * X;
else
ans = -1;
return ans;
}
public static void Main ( string [] args)
{
long N = 3;
Console.WriteLine(SmallestPerfectSquare(N));
}
}
|
Javascript
<script>
function SmallestPerfectSquare(N){
let X = 1e9;
let ans;
for (let i = 1; i < Math.sqrt(N); i++)
{
if (N % i == 0)
{
let a = i;
let b = N / i;
if ((b - a != 0) && ((b - a) % 2 == 0))
{
X = Math.min(X, (b - a) / 2);
}
}
}
if (X != 1e9)
ans = X * X;
else
ans = -1;
return ans;
}
let N = 3;
document.write(SmallestPerfectSquare(N));
</script>
|
Time complexity: O(sqrt(N)), since the loop runs from 0 to the square root of N the algorithm takes Sqrt(N) time to run efficiently
Auxiliary Space: O(1), since no extra array is used it takes up constant extra space