Skip to content

Commit 9b08e63

Browse files
committed
Reorderd power of 2
1 parent 18a92ff commit 9b08e63

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include<iostream>
2+
using namespace std;
3+
#include<algorithm>
4+
#include<string>
5+
#include<cmath>
6+
7+
string getSortedStr(int n) {
8+
string s = to_string(n);
9+
sort(begin(s), end(s));
10+
return s;
11+
}
12+
13+
bool reorderedPowerOf2(int n) {
14+
string s = getSortedStr(n);
15+
for(int p = 0; p <= 29; p++) {
16+
if(s == getSortedStr(1 << p)) {
17+
return true;
18+
}
19+
}
20+
return false;
21+
}
22+
int main(){
23+
int n;
24+
cout << "Enter a number: ";
25+
cin >> n;
26+
if(reorderedPowerOf2(n)){
27+
cout << n << " can be reordered to a power of 2." << endl;
28+
} else {
29+
cout << n << " cannot be reordered to a power of 2." << endl;
30+
}
31+
return 0;
32+
}

0 commit comments

Comments
 (0)