Perfect Cubes in a Range | Practice | GeeksforGeeks

Given two given numbers a and b where 1<= a <= b, find the perfect cubes between a and b (a and b inclusive).
Example 1:
Input: a = 1, b = 100
Output: 1 8 27 64
Explaination: These are the proper cubes between 
1 and 100.
Example 2:
Input: a = 24, b = 576
Output: 27 64 125 216 343 512
Explaination: These are the proper cubes between 
24 and 576.
Your Task:
You do not need to read input or print anything. Your task is to complete the function properCubes() which takes a and b as input parameters and returns the proper cubes between a and b. The function returns -1 if there is no proper cube between the given values.
Expected Time Complexity: O(cuberoot(b))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ a ≤ b ≤ 104 
0
Neeraj Kumar2 years ago
Neeraj Kumar
https://uploads.disquscdn.c...
Hii! This is my code, Solution in 0.01 seconds
0
Tokir Manva2 years ago
Tokir Manva
Execution Time:0.02Python3 code#codeimport mathfor _ in range(int(input())):    a,b=map(int,input().split())    min=math.ceil((a**(1/3)))    max=math.floor((b**(1/3)))    if (math.floor((b**(1/3)))+1)**3 == b:        max+=1    if min == max:        print(min**3)    elif min > max:        print("No")    else:        for i in range(min,max+1):            print(i**3,end=" ")        print()
0
gunateja2 years ago
gunateja
t=int(input(""))while(t):    a=[int(i) for i in input().split()]    m=a[0]    n=a[1]    y=0    for i in range(1,n):        k=pow(i,3)        if(k>n):            break        if(k>=m):            print(k,end=" ")            y=1    if(y==0):        print("No")    else:        print("\r")    t-=1
Correct Answer.Correct AnswerExecution Time:0.02
0
Mahima Mahendru2 years ago
Mahima Mahendru
simple 0.01s solution c+ + ;)
https://ide.geeksforgeeks.o...
0
Chandra Shekhar Joshi
This comment was deleted.
0
Paras Jain2 years ago
Paras Jain
even my dev c++ giving correct output for input 78 541 and GFG is telling it wrong
please check my code :
//Perfect Cube#include<stdio.h>int main(){int t;scanf("%d",&t);while(t--){int a,b,i,c,count=0;scanf("%d%d",&a,&b);for(i=1;i<=b/3;i++){c=i*i*i;if(c>=a&&c<=b){printf("%d ",c);count=count+1;}}if(count==0){    printf("No\n");}printf("\n");}}
0
Akash3 years ago
Akash
https://ide.geeksforgeeks.o...why it is giving wrong output for input 78 541 while submitting?in other compiler it is giving correct output.
0
Pankaj Gupta3 years ago
Pankaj Gupta
it is the best soloution in python. Complexity=O(n)t=0.08 sec
t=int(input())for i in range(t):    s,m=map(int,input().split())    c=0    for j in range(0,m//3):        if (j**3>=s and j**3<=m):            c=1            print(j**3,end=" ")
    if c==0:        print("No")    else:        print("\r")
0
Hodor
This comment was deleted.
0
shivani5 years ago
shivani
http://code.geeksforgeeks.o...what,s wrong in my code..???
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.