Easy C++ code with explanation || BEATS 100% solutions.
Intuition\nThe problem requires checking if the digits of a number n can be rearranged to form a power of 2. The key insight is that two numbers have the same d
# Intuition\nThe problem requires checking if the digits of a number n can be rearranged to form a power of 2. The key insight is that two numbers have the same digit arrangement if their sorted digit strings are identical.\n\n# Approach\nConvert n to a string and sort its digits.\n\nPrecompute sorted powers of 2.\n\nGenerate powers of 2 from 2^0 to 2^30.\nFor each power of 2, convert it to a string, sort its digits, and store the result.\nCheck for a match:\n\nCompare the sorted digits of n with each sorted power of 2.\nReturn true if a match is found; otherwise, return false.\n\n# Complexity\n- Time complexity:\nThe conversion and sorting of n takes O(logn) due to the string conversion for the worst case.\n\n- Space complexity:\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    bool reorderedPowerOf2(int n) {\n        vector<string>v;\n        string p=to_string(n);\n        sort(p.begin(),p.end());\n        for(int i=0;i<31;i++){\n            long long x=(1LL<<i);\n            string s=to_string(x);\n            sort(s.begin(),s.end());\n            v.push_back(s);\n        }\n        for(auto it:v){\n            if(it==p){\n                return true;\n            }\n        }\n        return false;\n    }\n};\n```
['C++']