[InterviewBit] Power Of Two Integers

Power Of Two Integers

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int Solution::isPower(int A) {
if(A == 1) return true;
vector<int> factor;
for(int i = 2; i <= sqrt(A); i++) {
if(A % i) continue;
int now = 0;
while(A % i == 0) {
A /= i;
now += 1;
}
factor.push_back(now);
}
if(A != 1) return false;
int g = factor[0];
for(int i = 1; i < factor.size(); i++) g = __gcd(factor[i], g);
return g > 1;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/11/PS/interviewbit/power-of-two-integers/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.