-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReorderedPowerOf2.cpp
More file actions
37 lines (35 loc) · 975 Bytes
/
ReorderedPowerOf2.cpp
File metadata and controls
37 lines (35 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//Time complexity=O(logn)
//Space complexity=O(1)
class Solution {
public:
// Count digit frequencies of a non-negative integer n
vector<int> freq(int n){
vector<int>res(10,0);
while(n>0){
res[n%10]++;
n=n/10;
}
return res;
}
// Compare two frequency arrays (size 10) for equality
bool compa(const vector<int>&a ,const vector<int>&b){
for(int i=0;i<10;i++){
if(a[i]!=b[i]){
return false;
}
}
return true;
}
bool reorderedPowerOf2(int n) {
// Frequency of digits in the input number n
vector<int>freq2=freq(n);
// Check all powers of two up to 2^30 (fits in 32-bit signed int ie till limit ie 10^9)
for(int i=0;i<30;i++){
int pow2=(int)pow(2,i); //2^i
if(compa(freq2,freq(pow2))){
return true;
}
}
return false;
}
};