Time Limit Error at 1024000000

interview-questions
Tags: #<Tag:0x00007f182b5c7278>

#1

Can anyone help me out with the code…

public class Solution {
public int isPower(int A) {
int exist = 0;
if(A != 1){
for(int i = 1;i<=Math.sqrt(A);i++){
for(int j = 2;j<=A/2;j++){
if(Math.pow(i,j) == A){
exist = 1;
return exist;
}
}
}
}else{
exist = 1;
}
return exist;
}
}


#2

Youll have to move backwards ie reducing A to power 1/2 to 1/3 etc


#3

Traverse your first For loop with i=2 . and give a break statement whenever Math.pow(i,j)>A .
It will work :+1:


#4

instead of dividing it by 2 you can go for dividing it with i (for more better solution)