forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReorderedPowerOf2.java
More file actions
33 lines (29 loc) · 815 Bytes
/
ReorderedPowerOf2.java
File metadata and controls
33 lines (29 loc) · 815 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
class Solution {
public boolean reorderedPowerOf2(int N) {
int[] inputDigitFreq = freqCount(N);
for(int i=0;i<31;i++){
int powerOf2 = (int)Math.pow(2, i);
int[] powerOf2FreqCount = freqCount(powerOf2);
if(compareArray(inputDigitFreq, powerOf2FreqCount)){
return true;
}
}
return false;
}
private boolean compareArray(int[] freqCount1,int[] freqCount2){
for(int i=0;i<10;i++){
if(freqCount1[i]!= freqCount2[i]){
return false;
}
}
return true;
}
private int[] freqCount(int n){
int[] digitFreq = new int[10];
while(n>0){
digitFreq[n%10]++;
n = n/10;
}
return digitFreq;
}
}