Given a number N, find the minimum number that needs to be added to or subtracted from N, to make it a perfect square. If the number is to be added, print it with a + sign, else if the number is to be subtracted, print it with a – sign.
Input: N = 14
Nearest perfect square before 14 = 9
Nearest perfect square after 14 = 16
Therefore 2 needs to be added to 14 to get the closest perfect square
Input: N = 18
Nearest perfect square before 18 = 16
Nearest perfect square after 18 = 25
Therefore 2 needs to be subtracted from 18 to get the closest perfect square
- Get the number.
- Find the square root of the number and convert the result as an integer.
- After converting the double value to integer, this will contain the root of the perfect square before N, i.e. floor(square root(N)).
- Then find the square of this number, which will be the perfect square before N.
- Find the root of the perfect square after N, i.e. the ceil(square root(N)).
- Then find the square of this number, which will be the perfect square after N.
- Check whether the square of floor value is nearest to N or the ceil value.
- If the square of floor value is nearest to N, print the difference with a -sign. Else print the difference between the square of the ceil value and N with a + sign.
Below is the implementation of the above approach:
2 0 -2
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.
- Least number to be added to or subtracted from N to make it a Perfect Cube
- Number of times the largest perfect square number can be subtracted from N
- Find smallest perfect square number A such that N + A is also a perfect square number
- Number of times the largest Perfect Cube can be subtracted from N
- Check if a number is a perfect square having all its digits as a perfect square
- Smallest number to be subtracted to convert given number to a palindrome
- Count numbers upto N which are both perfect square and perfect cube
- Check if there exists a prime number which gives Y after being repeatedly subtracted from X
- Find minimum number to be divided to make a number a perfect square
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Check if a number is perfect square without finding square root
- Minimum digits to remove to make a number Perfect Square
- Count of elements to be multiplied with integers to make each pair of Array a perfect square
- Minimum value to be added to X such that it is at least Y percent of N
- Find the minimum number to be added to N to make it a prime number
- Minimum number of Parentheses to be added to make it valid
- Minimum number to be added to all digits of X to make X > Y
- Find the minimum number to be added to N to make it a power of K
- Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal
- Previous perfect square and cube number smaller than number N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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