Reordered Power of 2 โ€” Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโ€”or scroll down to preview all options.

๐Ÿง  Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

๐Ÿ“– Passive Learning

Read the full explanation, examples, and starter code at your own pace.

๐ŸŽฏ Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

๐Ÿง  Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

๐Ÿ“– Select Passive to activate

Understanding Reordered Power of 2
Detailed explanation and reference materials
Problem Overview

Reordered Power of 2

Difficulty: ๐ŸŸก Medium
Tags: Permutations, Math, Bit Manipulation


๐Ÿ“œ Problem Statement

You are given an integer n.
You may reorder the digits of n in any way you want (including keeping them in the original order),
but the new number must not start with zero โŒ0.

Return true โœ… if and only if you can rearrange the digits so that the result is a power of two โšก.


๐Ÿ“Œ Rules & Clarifications

  • ๐Ÿ”„ You can rearrange the digits in any order.
  • ๐Ÿšซ No leading zeros are allowed in the result.
  • ๐Ÿงฎ A valid result must be equal to 2^k for some integer k โ‰ฅ 0.
  • ๐Ÿ” The function should return true or false.

๐Ÿ’ก Examples

Example 1

Input: n = 1
Output: true
Reason: 1 = 2^0 โ†’ already a power of two โœ…

Example 2

Input: n = 10
Output: false
Reason: Permutations โ†’ 10, 01(โŒ invalid) โ†’ none are powers of two.

๐ŸŽฏ Constraints

  • 1 <= n <= 10^9

๐Ÿ›  Approach / Hints

๐Ÿ’ก Key Insight:
Instead of generating all permutations (which can be huge),
compare the digit frequency of n with the digit frequency of all possible powers of two within the given range.

Steps:

  1. Generate all powers of two up to 2^30 (since 2^30 = 1073741824 > 10^9).
  2. Convert each power of two into a string and count digit occurrences.
  3. Count the digits in n.
  4. If nโ€™s digit count matches any precomputed power-of-two digit count โ†’ return true.
  5. Otherwise โ†’ return false.

โณ Complexity

  • Precomputation: O(1) โ†’ only 30 powers of two.
  • Digit counting: O(d) where d โ‰ค 10.
  • Overall: Constant time in practice ๐Ÿš€.

โ€” Written by Saurabh Patil โ€ข B.Tech CSE โ€ข Software Developer

Categories
sorting
hashing
java
Reference Link
https://drawtocode.org/problems/reordered-power-of-2

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now ๐ŸŽฎ

๐ŸŽฏ Select Challenge to activate

๐Ÿง  Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly ๐Ÿงฉ

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ? ย 

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

๐Ÿ”’ Locked steps cannot be moved. Only unlocked steps are draggable.

๐Ÿ”Š Enable sound for swap feedback and completion effects.

DrawToCode โ€” Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

Visualization Boardย |ย Problemsย |ย Aboutย |ย Privacy Policy
EmailLinkedInTwitterInstagramGitHub
ยฉ 2026 DrawToCode. All rights reserved.