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 is referred to the difference between N and the closest perfect square.
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.
- 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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a number is a perfect square having all its digits as a perfect square
- Find smallest perfect square number A such that N + A is also a perfect square number
- Count numbers upto N which are both perfect square and perfect cube
- Check if a number is perfect square without finding square root
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Minimum absolute difference of a number and its closest prime
- Distance of chord from center when distance between center and another equal length chord is given
- Find the side of the squares which are lined in a row, and distance between the centers of first and last square is given
- Previous perfect square and cube number smaller than number N
- Find minimum number to be divided to make a number a perfect square
- Check perfect square using addition/subtraction
- Number of times the largest perfect square number can be subtracted from N
- Perfect Square String
- Largest number that is not a perfect square
- Check if given number is perfect square
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Minimum digits to remove to make a number Perfect Square
- Check whether the number formed by concatenating two numbers is a perfect square or not
- Find the Next perfect square greater than a given number
- Largest factor of a given number which is a perfect square
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.