Number of times the largest perfect square number can be subtracted from N

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.Â
Examples:Â
Input: N = 85Â
Output: 2Â
First step, 85 – (9 * 9) = 4Â
Second step 4 – (2 * 2) = 0Input: N = 114Â
Output: 4Â
First step, 114 – (10 * 10) = 14Â
Second step 14 – (3 * 3) = 5Â
Third step 5 – (2 * 2) = 1Â
Fourth step 1 – (1 * 1) = 0Â
Brute force approach:
The brute force approach to solve this problem involves repeatedly subtracting the largest perfect square less than or equal to N from N until N becomes zero, and counting the number of steps taken. To find the largest perfect square less than or equal to N, we iterate over the integers from sqrt(N) down to 1, and check if the square of the current integer is less than or equal to N. Once we have found the largest perfect square less than or equal to N, we subtract it from N and increment the number of steps taken. We repeat this process until N becomes zero, and then return the number of steps taken.
Implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the count of stepsint countSteps(int n){Â Â Â Â int steps = 0;Â Â Â Â while (n > 0) {Â Â Â Â Â Â Â Â int i;Â Â Â Â Â Â Â Â for (i = sqrt(n); i >= 1; i--) {Â Â Â Â Â Â Â Â Â Â Â Â if (i*i <= n) {Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â break;Â Â Â Â Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â }Â Â Â Â Â Â Â Â n -= i*i;Â Â Â Â Â Â Â Â steps++;Â Â Â Â }Â Â Â Â return steps;}Â
Â
// Driver codeint main(){Â Â Â Â int n = 85;Â Â Â Â cout << countSteps(n);Â
    return 0;} |
Java
import java.lang.Math;Â
class Main {Â
  // Function to return the count of steps  static int countSteps(int n)  {    int steps = 0;    while (n > 0) {      int i = (int)Math.sqrt(n);Â
      // Find the largest perfect square less than or      // equal to n      while (i >= 1) {        if (i * i <= n) {          break;        }        i--;      }      // Subtract the largest perfect square from n      // and increment steps      n -= i * i;      steps++;    }    return steps;  }Â
  public static void main(String[] args)  {    // Driver code    int n = 85;    System.out.println(countSteps(n));  }} |
Python3
import mathÂ
# Function to return the count of stepsÂ
Â
def countSteps(n):    steps = 0    while n > 0:        i = int(math.sqrt(n))        # Find the largest perfect square less than or equal to n        while i >= 1:            if i*i <= n:                break            i -= 1        # Subtract the largest perfect square from n and increment steps        n -= i*i        steps += 1    return stepsÂ
Â
# Driver coden = 85print(countSteps(n)) |
C#
// C# implementation of the approachusing System;Â
class GFG {    // Function to return the count of steps    static int countSteps(int n)    {        int steps = 0;        while (n > 0) {            int i;            for (i = (int)Math.Sqrt(n); i >= 1; i--) {                if (i * i <= n) {                    break;                }            }            n -= i * i;            steps++;        }        return steps;    }Â
    // Driver code    static void Main()    {        int n = 85;        Console.WriteLine(countSteps(n));    }}// This code is contributed by user_dtewbxkn77n |
Javascript
function countsteps(n){Â Â let steps = 0;Â Â Â Â Â while (n > 0){Â Â Â Â let i = Number.parseInt(Math.sqrt(n));Â Â Â Â Â Â Â Â Â while (i>=1){Â Â Â Â Â Â if (i*i <= n){Â Â Â Â Â Â Â Â break;Â Â Â Â Â Â }Â Â Â Â Â Â i--;Â Â Â Â }Â Â Â Â n -= i*i;Â Â Â Â steps++;Â Â }Â Â return steps;}Â
let n = 85;console.log(countsteps(n)); |
2
Time Complexity: O(N * sqrt(N))
Space Complexity: O(1)
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:Â Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the count of stepsint countSteps(int n){Â
    // Variable to store the count of steps    int steps = 0;Â
    // Iterate while N > 0    while (n) {Â
        // Get the largest perfect square        // and subtract it from N        int largest = sqrt(n);        n -= (largest * largest);Â
        // Increment steps        steps++;    }Â
    // Return the required count    return steps;}Â
// Driver codeint main(){Â Â Â Â int n = 85;Â Â Â Â cout << countSteps(n);Â
    return 0;} |
Java
// Java implementation of the approach import java.lang.Math;Â
public class GfG{Â
    // Function to return the count of steps     static int countSteps(int n)     {         // Variable to store the count of steps         int steps = 0;              // Iterate while N > 0         while (n > 0) {                  // Get the largest perfect square             // and subtract it from N             int largest = (int)Math.sqrt(n);             n -= (largest * largest);                  // Increment steps             steps++;         }              // Return the required count         return steps;     }Â
     public static void main(String []args){                 int n = 85;         System.out.println(countSteps(n));     }}Â
// This code is contributed by Rituraj Jain |
Python3
# Python3 implementation of the approachfrom math import sqrt Â
# Function to return the count of stepsdef countSteps(n) :Â
    # Variable to store the count of steps    steps = 0;Â
    # Iterate while N > 0    while (n) :Â
        # Get the largest perfect square        # and subtract it from N        largest = int(sqrt(n));        n -= (largest * largest);Â
        # Increment steps        steps += 1;Â
    # Return the required count    return steps;     # Driver codeif __name__ == "__main__" :Â
    n = 85;    print(countSteps(n));     # This code is contributed by Ryuga |
C#
// C# implementation of the approach using System;Â
class GfG{Â
    // Function to return the count of steps     static int countSteps(int n)     {         // Variable to store the count of steps         int steps = 0;              // Iterate while N > 0         while (n > 0)         {                  // Get the largest perfect square             // and subtract it from N             int largest = (int)Math.Sqrt(n);             n -= (largest * largest);                  // Increment steps             steps++;         }              // Return the required count         return steps;     }Â
    // Driver code    public static void Main()    {        int n = 85;         Console.WriteLine(countSteps(n));    }}Â
// This code is contributed by Code_Mech. |
PHP
<?php// PHP implementation of the approachÂ
// Function to return the count of stepsfunction countSteps($n){Â
    // Variable to store the count of steps    $steps = 0;Â
    // Iterate while N > 0    while ($n)     {Â
        // Get the largest perfect square        // and subtract it from N        $largest = (int)sqrt($n);        $n -= ($largest * $largest);Â
        // Increment steps        $steps++;    }Â
    // Return the required count    return $steps;}Â
// Driver code$n = 85;echo countSteps($n);Â
// This code is contributed // by Akanksha Rai?> |
Javascript
<script>Â
// JavaScript implementation of the approachÂ
// Function to return the count of stepsfunction countSteps(n){         // Variable to store the count of steps    let steps = 0;Â
    // Iterate while N > 0    while (n)     {                 // Get the largest perfect square        // and subtract it from N        let largest = Math.floor(Math.sqrt(n));        n -= (largest * largest);Â
        // Increment steps        steps++;    }Â
    // Return the required count    return steps;}Â
// Driver codeÂ
let n = 85;Â
document.write(countSteps(n));Â
// This code is contributed by Manoj.Â
</script> |
2
Â
Time Complexity: O(logn) because inbuilt sqrt function has been used
Auxiliary Space: O(1), since no extra space has been taken.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



