
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Reordered Power of 2 in Python
Suppose we have a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is non-zero. We have to check whether we can do this in a way such that the resulting number is a power of 2.
So, if the input is like N = 812, then the output will be True
To solve this, we will follow these steps −
i:= 1
-
while i<=1000000000, do
s:= i as a string
s:= sort characters of s
t:= n as a string
t:= sort characters of t
-
if s is same as t, then
return True
i:= i*2
return False
Example
Let us see the following implementation to get better understanding −
def solve(n): i=1 while i<=1000000000: s=str(i) s=''.join(sorted(s)) t=str(n) t=''.join(sorted(t)) if s==t: return True i=i*2 return False N = 812 print(solve(N))
Input
812
Output
True
Advertisements