Given a positive integer
. The task is to find the perfect square number closest to N and steps required to reach this number from N.
Note: The closest perfect square to N can be either less than, equal to or greater than N and steps are referred to as the difference between N and the closest perfect square.
Examples:
Input: N = 1500
Output: Perfect square = 1521, Steps = 21
For N = 1500
Closest perfect square greater than N is 1521.
So steps required is 21.
Closest perfect square less than N is 1444.
So steps required is 56.
The minimum of these two is 1521 with steps 21.
Input: N = 2
Output: Perfect Square = 1, Steps = 1
For N = 2
Closest perfect square greater than N is 4.
So steps required is 2.
Closest perfect square less than N is 1.
So steps required is 1.
The minimum of these two is 1.
Approach 1:
- If N is a perfect square then print N and steps as 0.
- Else, find the first perfect square number > N and note its difference with N.
- Then, find the first perfect square number < N and note its difference with N.
- And print the perfect square resulting in the minimum of these two differences obtained and also the difference as the minimum steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPerfect( int N)
{
if (( sqrt (N) - floor ( sqrt (N))) != 0)
return false ;
return true ;
}
void getClosestPerfectSquare( int N)
{
if (isPerfect(N)) {
cout << N << " "
<< "0" << endl;
return ;
}
int aboveN = -1, belowN = -1;
int n1;
n1 = N + 1;
while ( true ) {
if (isPerfect(n1)) {
aboveN = n1;
break ;
}
else
n1++;
}
n1 = N - 1;
while ( true ) {
if (isPerfect(n1)) {
belowN = n1;
break ;
}
else
n1--;
}
int diff1 = aboveN - N;
int diff2 = N - belowN;
if (diff1 > diff2)
cout << belowN << " " << diff2;
else
cout << aboveN << " " << diff1;
}
int main()
{
int N = 1500;
getClosestPerfectSquare(N);
}
|
Java
class GFG {
static boolean isPerfect( int N)
{
if ((Math.sqrt(N) - Math.floor(Math.sqrt(N))) != 0 )
return false ;
return true ;
}
static void getClosestPerfectSquare( int N)
{
if (isPerfect(N)) {
System.out.println(N + " "
+ "0" );
return ;
}
int aboveN = - 1 , belowN = - 1 ;
int n1;
n1 = N + 1 ;
while ( true ) {
if (isPerfect(n1)) {
aboveN = n1;
break ;
}
else
n1++;
}
n1 = N - 1 ;
while ( true ) {
if (isPerfect(n1)) {
belowN = n1;
break ;
}
else
n1--;
}
int diff1 = aboveN - N;
int diff2 = N - belowN;
if (diff1 > diff2)
System.out.println(belowN + " " + diff2);
else
System.out.println(aboveN + " " + diff1);
}
public static void main(String args[])
{
int N = 1500 ;
getClosestPerfectSquare(N);
}
}
|
Python3
from math import sqrt, floor
def isPerfect(N):
if (sqrt(N) - floor(sqrt(N)) ! = 0 ):
return False
return True
def getClosestPerfectSquare(N):
if (isPerfect(N)):
print (N, "0" )
return
aboveN = - 1
belowN = - 1
n1 = 0
n1 = N + 1
while ( True ):
if (isPerfect(n1)):
aboveN = n1
break
else :
n1 + = 1
n1 = N - 1
while ( True ):
if (isPerfect(n1)):
belowN = n1
break
else :
n1 - = 1
diff1 = aboveN - N
diff2 = N - belowN
if (diff1 > diff2):
print (belowN, diff2)
else :
print (aboveN, diff1)
N = 1500
getClosestPerfectSquare(N)
|
C#
using System;
class GFG {
static bool isPerfect( int N)
{
if ((Math.Sqrt(N) - Math.Floor(Math.Sqrt(N))) != 0)
return false ;
return true ;
}
static void getClosestPerfectSquare( int N)
{
if (isPerfect(N)) {
Console.WriteLine(N + " "
+ "0" );
return ;
}
int aboveN = -1, belowN = -1;
int n1;
n1 = N + 1;
while ( true ) {
if (isPerfect(n1)) {
aboveN = n1;
break ;
}
else
n1++;
}
n1 = N - 1;
while ( true ) {
if (isPerfect(n1)) {
belowN = n1;
break ;
}
else
n1--;
}
int diff1 = aboveN - N;
int diff2 = N - belowN;
if (diff1 > diff2)
Console.WriteLine(belowN + " " + diff2);
else
Console.WriteLine(aboveN + " " + diff1);
}
public static void Main()
{
int N = 1500;
getClosestPerfectSquare(N);
}
}
|
PHP
<?php
function isPerfect( $N )
{
if ((sqrt( $N ) - floor (sqrt( $N ))) != 0)
return false;
return true;
}
function getClosestPerfectSquare( $N )
{
if (isPerfect( $N ))
{
echo $N , " " , "0" , "\n" ;
return ;
}
$aboveN = -1;
$belowN = -1;
$n1 ;
$n1 = $N + 1;
while (true)
{
if (isPerfect( $n1 ))
{
$aboveN = $n1 ;
break ;
}
else
$n1 ++;
}
$n1 = $N - 1;
while (true)
{
if (isPerfect( $n1 ))
{
$belowN = $n1 ;
break ;
}
else
$n1 --;
}
$diff1 = $aboveN - $N ;
$diff2 = $N - $belowN ;
if ( $diff1 > $diff2 )
echo $belowN , " " , $diff2 ;
else
echo $aboveN , " " , $diff1 ;
}
$N = 1500;
getClosestPerfectSquare( $N );
?>
|
Javascript
<script>
function isPerfect(N)
{
if ((Math.sqrt(N) -
Math.floor(Math.sqrt(N))) != 0)
return false ;
return true ;
}
function getClosestPerfectSquare(N)
{
if (isPerfect(N)) {
document.write(N + " " + "0" + "</br>" );
return ;
}
let aboveN = -1, belowN = -1;
let n1;
n1 = N + 1;
while ( true ) {
if (isPerfect(n1)) {
aboveN = n1;
break ;
}
else
n1++;
}
n1 = N - 1;
while ( true ) {
if (isPerfect(n1)) {
belowN = n1;
break ;
}
else
n1--;
}
let diff1 = aboveN - N;
let diff2 = N - belowN;
if (diff1 > diff2)
document.write(belowN + " " + diff2);
else
document.write(aboveN + " " + diff1);
}
let N = 1500;
getClosestPerfectSquare(N);
</script>
|
Time Complexity: O(n), where n is the given number since we are using brute force to find the above and below perfect squares.
Auxiliary Space: O(1), since no extra space has been taken.
Approach 2:
The above method might take a lot of time for bigger numbers i.e. greater than 106. We would wish to have a faster way to do that.
- Here, we will use maths to solve the above problem in constant time complexity.
- We will first find the square root of the number n.
- We will check if n was a perfect square, and if it was, we will return 0 there itself.
- Else, we will use its square root to find the just above and below perfect square numbers, and return the one which is at the minimum distance.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void getClosestPerfectSquare( int N)
{
int x = sqrt (N);
if ((x*x)==N){
cout<<N<< " " <<0;
return ;
}
int aboveN = (x+1)*(x+1), belowN = x*x;
int diff1 = aboveN - N;
int diff2 = N - belowN;
if (diff1 > diff2)
cout << belowN << " " << diff2;
else
cout << aboveN << " " << diff1;
}
int main()
{
int N = 1500;
getClosestPerfectSquare(N);
}
|
Java
public class GFG {
static void getClosestPerfectSquare( int N)
{
int x = ( int )Math.sqrt(N);
if ((x*x)==N){
System.out.println(N);
return ;
}
int aboveN = (x+ 1 )*(x+ 1 ), belowN = x*x;
int diff1 = aboveN - N;
int diff2 = N - belowN;
if (diff1 > diff2)
System.out.println(belowN+ " " +diff2);
else
System.out.println(aboveN+ " " +diff1);
}
public static void main (String[] args)
{
int N = 1500 ;
getClosestPerfectSquare(N);
}
}
|
Python3
from math import sqrt, floor
def getClosestPerfectSquare(N):
x = floor(sqrt(N))
if (sqrt(N) - floor(sqrt(N)) = = 0 ):
print (N, 0 )
return
aboveN = (x + 1 ) * (x + 1 )
belowN = x * x
diff1 = aboveN - N
diff2 = N - belowN
if (diff1 > diff2):
print (belowN, diff2)
else :
print (aboveN, diff1)
N = 1500
getClosestPerfectSquare(N)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void getClosestPerfectSquare( int N)
{
int x = ( int )Math.Sqrt(N);
if ((x*x)==N){
Console.WriteLine(N);
return ;
}
int aboveN = (x+1)*(x+1), belowN = x*x;
int diff1 = aboveN - N;
int diff2 = N - belowN;
if (diff1 > diff2)
Console.WriteLine(belowN+ " " +diff2);
else
Console.WriteLine(aboveN+ " " +diff1);
}
public static void Main ( string [] args)
{
int N = 1500;
getClosestPerfectSquare(N);
}
}
|
Javascript
function getClosestPerfectSquare(N)
{
let x = Math.floor(Math.sqrt(N))
if (Math.sqrt(N) - Math.floor(Math.sqrt(N)) == 0)
{
console.log(N,0)
return
}
let aboveN = (x+1)*(x+1)
let belowN = x*x
let diff1 = aboveN - N
let diff2 = N - belowN
if (diff1 > diff2)
console.log(belowN, diff2)
else
console.log(aboveN, diff1)
}
let N = 1500
getClosestPerfectSquare(N)
|
Time Complexity: O(logN), as it is using inbuilt sqrt function
Auxiliary Space: O(1), since no extra space has been taken.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Oct, 2022
Like Article
Save Article