Perfect cube greater than a given number
Given a number N, the task is to find the next perfect cube greater than N.
Examples:
Input: N = 6 Output: 8 8 is a greater number than 6 and is also a perfect cube Input: N = 9 Output: 27
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
- Find the cube root of given N.
- Calculate its floor value using floor function in C++.
- Then add 1 to it.
- Print cube of that number.
C++
// C++ implementation of above approach#include <cmath>#include <iostream>using namespace std;// Function to find the next perfect cubeint nextPerfectCube(int N){ int nextN = floor(cbrt(N)) + 1; return nextN * nextN * nextN;}// Driver Codeint main(){ int n = 35; cout << nextPerfectCube(n); return 0;} |
Java
//Java implementation of above approachimport java.util.*;import java.lang.*;import java.io.*;class GFG{// Function to find the next perfect cubestatic int nextPerfectCube(int N){ int nextN = (int)Math.floor(Math.cbrt(N)) + 1; return nextN * nextN * nextN;} // Driver Codepublic static void main(String args[]){ int n = 35; System.out.print(nextPerfectCube(n));}} |
Python 3
# Python 3 implementation of above approach# from math import everythingfrom math import *# Function to find the next perfect cubedef nextPerfectCube(N) : nextN = floor(N ** (1/3)) + 1 return nextN ** 3# Driver code if __name__ == "__main__" : n = 35 print(nextPerfectCube(n))# This code is contributed by ANKITRAI1 |
C#
// C# implementation of above approachusing System;class GFG{// Function to find the next perfect cubestatic int nextPerfectCube(int N){ int nextN = (int)Math.Floor(Math.Pow(N, (double)1/3)) + 1; return nextN * nextN * nextN;}// Driver Codepublic static void Main(){ int n = 35; Console.Write(nextPerfectCube(n));}}// This code is contributed by ChitraNayal |
PHP
<?php// PHP implementation of above approach// from math import everything// Function to find the next perfect cubefunction nextPerfectCube($N){ $nextN = (int)(floor(pow($N,(1/3))) + 1); return $nextN * $nextN * $nextN ;}// Driver code $n = 35; print(nextPerfectCube($n));// This code is contributed by mits?> |
Javascript
<script>// Javascript implementation of above approach// Function to find the next perfect cubefunction nextPerfectCube(N){ let nextN = Math.floor(Math.cbrt(N)) + 1; return nextN * nextN * nextN;}// Driver Codelet n = 35;document.write(nextPerfectCube(n));</script> |
Output:
64
Time Complexity: O(1)
Auxiliary Space: O(1)


