Given a number N. At every step, subtract the largest perfect square( ≤ N) from N. Repeat this step while N > 0. The task is to count the number of steps that can be performed.
Input: N = 85
First step, 85 – (9 * 9) = 4
Second step 4 – (2 * 2) = 0
Input: N = 114
First step, 114 – (10 * 10) = 14
Second step 14 – (3 * 3) = 5
Third step 5 – (2 * 2) = 1
Fourth step 1 – (1 * 1) = 0
Approach: Iteratively subtract the largest perfect square (≤ N) from N while N > 0 and count the number of 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.
- Number of times the largest Perfect Cube can be subtracted from N
- Least number to be added to or subtracted from N to make it a Perfect Square
- Largest number that is not a perfect square
- Largest factor of a given number which is a perfect square
- Largest perfect square number in an Array
- Largest N digit Octal number which is a Perfect square
- Largest Divisor of a Number not divisible by a perfect square
- Find the Largest N digit perfect square number in Base B
- Least number to be added to or subtracted from N to make it a Perfect Cube
- Find smallest perfect square number A such that N + A is also a perfect square number
- Find minimum number to be divided to make a number a perfect square
- Previous perfect square and cube number smaller than number N
- 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
- Perfect Square factors of a Number
- Check if given number is perfect square
- Find the Next perfect square greater than a given number
- Check whether the number can be made perfect square after adding K
- Check whether the number can be made perfect square after adding 1
- Check if a given number is a Perfect square using Binary Search
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.