Program to find Reordered Power of 2 in Python

PythonServer Side ProgrammingProgramming

<p>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.</p><p>So, if the input is like N = 812, then the output will be True</p><p>To solve this, we will follow these steps &minus;</p><ul class="list"><li><p>i:= 1</p></li><li><p>while i&lt;=1000000000, do</p><ul class="list"><li><p>s:= i as a string</p></li><li><p>s:= sort characters of s</p></li><li><p>t:= n as a string</p></li><li><p>t:= sort characters of t</p></li><li><p>if s is same as t, then</p><ul class="list"><li><p>return True</p></li></ul></li><li><p>i:= i*2</p></li></ul></li><li><p>return False</p></li></ul><h2>Example</h2><p>Let us see the following implementation to get better understanding &minus;</p><pre class="demo-code notranslate language-python" data-lang="python">def solve(n): i=1 while i&lt;=1000000000: s=str(i) s=&#39;&#39;.join(sorted(s)) t=str(n) t=&#39;&#39;.join(sorted(t)) if s==t: return True i=i*2 return False N = 812 print(solve(N)) </pre><h2>Input</h2><pre class="just-code notranslate language-python" data-lang="python">812</pre><h2>Output</h2><pre class="result notranslate">True </pre>
raja
Updated on 06-Oct-2021 11:41:39

Advertisements