Code is giving TLE for 10240000 plz help anyone [Solved]


#1

[Edited]

type of B should be long long int because if A is at the border of INT_MAX then B will overflow according the implementation this will results in -ve value of B, thus will give TLE.

bool isValid(int A,int Q)
{
    int B=1; // this should be long long int
    while(B<A)
    {
        B *= Q;
    }
    return B == A;
}
int Solution::isPower(int A) {
    if(A==1) return true;
    for(int i=2;i<=sqrt(A);i++)
    {
        if(isValid(A,i))
        {
            return true;
        }
    }
    return false;
}