Skip to content
Related Articles

Related Articles

Closest perfect square and its distance
  • Difficulty Level : Easy
  • Last Updated : 10 May, 2021

Given a positive integer N  . 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 is referred to 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: 
 

  • If N is a perefct 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++




// CPP program to find the closest perfect square
// taking minimum steps to reach from a number
 
#include<bits/stdc++.h>
using namespace std;
 
 
    // Function to check if a number is
    // perfect square or not
    bool isPerfect(int N)
    {
        if ((sqrt(N) - floor(sqrt(N))) != 0)
            return false;
        return true;
    }
 
    // Function to find the closest perfect square
    // taking minimum steps to reach from a number
    void getClosestPerfectSquare(int N)
    {
        if (isPerfect(N))
        {
            cout<<N<<" "<<"0"<<endl;
            return;
        }
 
        // Variables to store first perfect
        // square number
        // above and below N
        int aboveN = -1, belowN = -1;
        int n1;
 
        // Finding first perfect square
        // number greater than N
        n1 = N + 1;
        while (true) {
            if (isPerfect(n1)) {
                aboveN = n1;
                break;
            }
            else
                n1++;
        }
 
        // Finding first perfect square
        // number less than N
        n1 = N - 1;
        while (true) {
            if (isPerfect(n1)) {
                belowN = n1;
                break;
            }
            else
                n1--;
        }
 
        // Variables to store the differences
        int diff1 = aboveN - N;
        int diff2 = N - belowN;
 
        if (diff1 > diff2)
            cout<<belowN<<" "<<diff2;
        else
            cout<<aboveN<<" "<<diff1;
    }
 
    // Driver code
    int main()
    {
        int N = 1500;
 
        getClosestPerfectSquare(N);
    }
//This code is contributed by
// Surendra_Gangwar

Java




// Java program to find the closest perfect square
// taking minimum steps to reach from a number
 
class GFG {
 
    // Function to check if a number is
    // perfect square or not
    static boolean isPerfect(int N)
    {
        if ((Math.sqrt(N) - Math.floor(Math.sqrt(N))) != 0)
            return false;
        return true;
    }
 
    // Function to find the closest perfect square
    // taking minimum steps to reach from a number
    static void getClosestPerfectSquare(int N)
    {
        if (isPerfect(N)) {
            System.out.println(N + " "
                               + "0");
            return;
        }
 
        // Variables to store first perfect
        // square number
        // above and below N
        int aboveN = -1, belowN = -1;
        int n1;
 
        // Finding first perfect square
        // number greater than N
        n1 = N + 1;
        while (true) {
            if (isPerfect(n1)) {
                aboveN = n1;
                break;
            }
            else
                n1++;
        }
 
        // Finding first perfect square
        // number less than N
        n1 = N - 1;
        while (true) {
            if (isPerfect(n1)) {
                belowN = n1;
                break;
            }
            else
                n1--;
        }
 
        // Variables to store the differences
        int diff1 = aboveN - N;
        int diff2 = N - belowN;
 
        if (diff1 > diff2)
            System.out.println(belowN + " " + diff2);
        else
            System.out.println(aboveN + " " + diff1);
    }
 
    // Driver code
    public static void main(String args[])
    {
        int N = 1500;
 
        getClosestPerfectSquare(N);
    }
}

Python3




# Python3 program to find the closest
# perfect square taking minimum steps
# to reach from a number
 
# Function to check if a number is
# perfect square or not
from math import sqrt, floor
def isPerfect(N):
    if (sqrt(N) - floor(sqrt(N)) != 0):
        return False
    return True
 
# Function to find the closest perfect square
# taking minimum steps to reach from a number
def getClosestPerfectSquare(N):
    if (isPerfect(N)):
        print(N, "0")
        return
 
    # Variables to store first perfect
    # square number above and below N
    aboveN = -1
    belowN = -1
    n1 = 0
 
    # Finding first perfect square
    # number greater than N
    n1 = N + 1
    while (True):
        if (isPerfect(n1)):
            aboveN = n1
            break
        else:
            n1 += 1
 
    # Finding first perfect square
    # number less than N
    n1 = N - 1
    while (True):
        if (isPerfect(n1)):
            belowN = n1
            break
        else:
            n1 -= 1
             
    # Variables to store the differences
    diff1 = aboveN - N
    diff2 = N - belowN
 
    if (diff1 > diff2):
        print(belowN, diff2)
    else:
        print(aboveN, diff1)
 
# Driver code
N = 1500
getClosestPerfectSquare(N)
 
# This code is contributed
# by sahishelangia

C#




// C# program to find the closest perfect square
// taking minimum steps to reach from a number
using System;
 
class GFG {
 
    // Function to check if a number is
    // perfect square or not
    static bool isPerfect(int N)
    {
        if ((Math.Sqrt(N) - Math.Floor(Math.Sqrt(N))) != 0)
            return false;
        return true;
    }
 
    // Function to find the closest perfect square
    // taking minimum steps to reach from a number
    static void getClosestPerfectSquare(int N)
    {
        if (isPerfect(N)) {
            Console.WriteLine(N + " "
                            + "0");
            return;
        }
 
        // Variables to store first perfect
        // square number
        // above and below N
        int aboveN = -1, belowN = -1;
        int n1;
 
        // Finding first perfect square
        // number greater than N
        n1 = N + 1;
        while (true) {
            if (isPerfect(n1)) {
                aboveN = n1;
                break;
            }
            else
                n1++;
        }
 
        // Finding first perfect square
        // number less than N
        n1 = N - 1;
        while (true) {
            if (isPerfect(n1)) {
                belowN = n1;
                break;
            }
            else
                n1--;
        }
 
        // Variables to store the differences
        int diff1 = aboveN - N;
        int diff2 = N - belowN;
 
        if (diff1 > diff2)
            Console.WriteLine(belowN + " " + diff2);
        else
            Console.WriteLine(aboveN + " " + diff1);
    }
 
    // Driver code
    public static void Main()
    {
        int N = 1500;
 
        getClosestPerfectSquare(N);
    }
}
// This code is contributed by anuj_67..

PHP




<?php
// PHP program to find the closest perfect
// square taking minimum steps to reach
// from a number
 
// Function to check if a number is
// perfect square or not
function isPerfect($N)
{
    if ((sqrt($N) - floor(sqrt($N))) != 0)
        return false;
    return true;
}
 
// Function to find the closest perfect square
// taking minimum steps to reach from a number
function getClosestPerfectSquare($N)
{
    if (isPerfect($N))
    {
        echo $N, " ", "0", "\n";
        return;
    }
 
    // Variables to store first perfect
    // square number
    // above and below N
    $aboveN = -1;
    $belowN = -1;
    $n1;
 
    // Finding first perfect square
    // number greater than N
    $n1 = $N + 1;
    while (true)
    {
        if (isPerfect($n1))
        {
            $aboveN = $n1;
            break;
        }
        else
            $n1++;
    }
 
    // Finding first perfect square
    // number less than N
    $n1 = $N - 1;
    while (true)
    {
        if (isPerfect($n1))
        {
            $belowN = $n1;
            break;
        }
        else
            $n1--;
    }
 
    // Variables to store the differences
    $diff1 = $aboveN - $N;
    $diff2 = $N - $belowN;
 
    if ($diff1 > $diff2)
        echo $belowN, " " , $diff2;
    else
        echo $aboveN, " ", $diff1;
}
 
// Driver code
$N = 1500;
getClosestPerfectSquare($N);
 
// This code is contributed by ajit.
?>

Javascript




<script>
 
    // Javascript program to find
    // the closest perfect square
    // taking minimum steps to reach
    // from a number
     
    // Function to check if a number is
    // perfect square or not
    function isPerfect(N)
    {
        if ((Math.sqrt(N) -
        Math.floor(Math.sqrt(N))) != 0)
            return false;
        return true;
    }
   
    // Function to find the closest perfect square
    // taking minimum steps to reach from a number
    function getClosestPerfectSquare(N)
    {
        if (isPerfect(N)) {
            document.write(N + " " + "0" + "</br>");
            return;
        }
   
        // Variables to store first perfect
        // square number
        // above and below N
        let aboveN = -1, belowN = -1;
        let n1;
   
        // Finding first perfect square
        // number greater than N
        n1 = N + 1;
        while (true) {
            if (isPerfect(n1)) {
                aboveN = n1;
                break;
            }
            else
                n1++;
        }
   
        // Finding first perfect square
        // number less than N
        n1 = N - 1;
        while (true) {
            if (isPerfect(n1)) {
                belowN = n1;
                break;
            }
            else
                n1--;
        }
   
        // Variables to store the differences
        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>
Output: 
1521 21

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :