Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmNumber of times the largest Perfect Cube can be subtracted from N

Number of times the largest Perfect Cube can be subtracted from N

Given a number N, at every step, subtract the largest perfect cube( ? 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 = 100 
Output:
First step, 100 – (4 * 4 * 4) = 100 – 64 = 36 
Second step, 36 – (3 * 3 * 3) = 36 – 27 = 9 
Third step, 9 – (2 * 2 * 2) = 9 – 8 = 1 
Fourth step, 1 – (1 * 1 * 1) = 1 – 1 = 0

Input: N = 150 
Output:
First step, 150 – (5 * 5 * 5) = 150 – 125 = 25 
Second step, 25 – (2 * 2 * 2) = 25 – 8 = 17 
Third step, 17 – (2 * 2 * 2) = 17 – 8 = 9 
Fourth step, 9 – (2 * 2 * 2) = 9 – 8 = 1 
Fifth step, 1 – (1 * 1 * 1) = 1 – 1 = 0 

Approach:  

  • Get the number from which the largest perfect cube has to be reduced.
  • Find the cube root of the number and convert the result as an integer. The cube root of the number might contain some fraction part after the decimal, which needs to be avoided.
  • Subtract the cube of the integer found in the previous step. This would remove the largest possible perfect cube from the number in the above step. 
N = N - ((int) ?N)3
  • Repeat the above two steps with the reduced number, till it is greater than 0.
  • Print the number of times a perfect cube has been reduced from N. This is the final result.

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 steps
int countSteps(int n)
{
 
    // Variable to store the count of steps
    int steps = 0;
 
    // Iterate while N > 0
    while (n) {
 
        // Get the largest perfect cube
        // and subtract it from N
        int largest = cbrt(n);
        n -= (largest * largest * largest);
 
        // Increment steps
        steps++;
    }
 
    // Return the required count
    return steps;
}
 
// Driver code
int main()
{
    int n = 150;
    cout << countSteps(n);
 
    return 0;
}


Java




// Java implementation of the approach
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 cube
        // and subtract it from N
        int largest = (int) Math.cbrt(n);
        n -= (largest * largest * largest);
  
        // Increment steps
        steps++;
    }
  
    // Return the required count
    return steps;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 150;
    System.out.print(countSteps(n));
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python3 implementation of the approach
from math import floor
 
# Function to return the count of steps
def countSteps(n):
 
    # Variable to store the count of steps
    steps = 0
 
    # Iterate while N > 0
    while (n):
 
        # Get the largest perfect cube
        # and subtract it from N
        largest = floor(n**(1/3))
        n -= (largest * largest * largest)
 
        # Increment steps
        steps += 1
 
    # Return the required count
    return steps
 
# Driver code
n = 150
print(countSteps(n))
 
# This code is contributed by mohit kumar 29


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 cube
        // and subtract it from N
        int largest = (int) Math.Pow(n,(double)1/3);
        n -= (largest * largest * largest);
   
        // Increment steps
        steps++;
    }
   
    // Return the required count
    return steps;
}
   
// Driver code
public static void Main(String[] args)
{
    int n = 150;
    Console.Write(countSteps(n));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript




<script>
 
// JavaScript implementation of the approach
 
// Function to return the count of steps
function countSteps(n)
{
     
    // Variable to store the count of steps
    let steps = 0;
 
    // Iterate while N > 0
    while (n)
    {
         
        // Get the largest perfect cube
        // and subtract it from N
        let largest = Math.floor(Math.cbrt(n));
        n -= (largest * largest * largest);
 
        // Increment steps
        steps++;
    }
 
    // Return the required count
    return steps;
}
 
// Driver code
let n = 150;
 
document.write(countSteps(n));
 
// This code is contributed by Manoj.
 
</script>


Output: 

5

 

Time complexity: O(logn), as using inbuilt cbrt function
Auxiliary space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
17 Oct, 2022
Like Article
Save Article


Previous


Next


Share your thoughts in the comments

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments