Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

869. Reordered Power of 2

Medium62.4% Acceptance
Hash TableMathSorting
Asked by:
A
Amazon
ProblemSolutions (12)VideosCompanies (5)Notes

Problem Statement

You are given an integer n. We reorder the digits in any order (including the original order) such that the leading digit is not zero.

Return true if and only if we can do this so that the resulting number is a power of two.

Example 1:

Input: n = 1
Output: true

Example 2:

Input: n = 10
Output: false

Constraints:

  • 1 <= n <= 109

Approach

In #869 Reordered Power of 2, the goal is to determine whether the digits of an integer n can be rearranged to form a power of two. A key observation is that if two numbers contain the same digits, they will have the same digit frequency or the same sorted digit sequence.

A common approach is to enumerate all powers of two within the integer range and compare their digit patterns with that of n. For each candidate power of two, either sort its digits or build a digit frequency count. If the pattern matches the one computed for n, then a valid reordering exists.

Using a counting-based signature (e.g., frequency of digits 0–9) is typically faster than sorting for repeated checks. Since there are only about 30 powers of two within the 32-bit integer range, the enumeration remains efficient. This makes the solution practical with near-constant time complexity and small auxiliary space.

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
G
Google
M
Microsoft
A
Adobe
A
Apple

Complexity

ApproachTime ComplexitySpace Complexity
Enumerate powers of 2 with sorted digit comparisonO(k log k * log N)O(k)
Digit frequency counting (hash/count signature)O(k * log N)O(1)

Video Solution Available

Algorithms Made Easy

View all video solutions

Solutions (12)

Digit Count Matching with Powers of Two

One approach to solve the problem is to leverage the fact that two numbers with the same digits in different orders will have the same digit frequency (or count of each digit). For the given number n, we aim to check if its digits can be rearranged to form any power of two. To achieve this:

  • Precompute all powers of two up to 10^9 and store their digit frequency in a set.
  • For the input number n, calculate its digit frequency and check if it exists in the set.

Time Complexity: O(1) because the number of power of twos checked (31) and the frequency array size (10) are constant.
Space Complexity: O(1) as only small fixed-size integer arrays are used.

CC++JavaPythonC#JavaScript
1#include <stdbool.h>
2#include <stdio.h>
3#include <string.h>
4
5bool reorderedPowerOf2(int n) {
6    




Explanation

The implementation uses an array to count the frequency of each digit in n. It then iterates through all powers of two that can exist within the limits (2^0 to 2^30). For each power, it calculates the digit frequency and compares it to n's. If a match is found, it returns true. If no match is found through all powers, it returns false.

Backtracking Approach

A backtracking method can be employed by generating permutations of the digits of the number n and checking if any results in a power of two. Given the constraint, this method needs optimization to efficiently discard invalid permutations early.

  • Generate all valid permutations of n's digits where the leading digit isn't zero.
  • Track numbers that form powers of two during permutations.

Not provided due to complexity constraints in inline format.

CC++JavaPythonC#JavaScript
1

Video Solutions

Watch expert explanations and walkthroughs

Reordered Power of 2 | Live Coding with Explanation | Leetcode - 869

Algorithms Made Easy
4:486,070 views

Asked By Companies

5 companies
A
Amazon
G
Google
M
Microsoft
A
Adobe
A
Apple

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Integer to RomanMedium
Roman to IntegerEasy
Max Points on a LineHard
Fraction to Recurring DecimalMedium
More similar problems

Related Topics

Hash TableMathSortingCountingEnumeration

Problem Stats

Acceptance Rate62.4%
DifficultyMedium
Companies5

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Reordered Power of 2 asked in FAANG interviews?

Yes, problems like Reordered Power of 2 are common in technical interviews because they test pattern recognition, counting techniques, and efficient comparison strategies. It also evaluates how well candidates avoid brute-force permutation generation.

Why do we enumerate powers of two in this problem?

Instead of trying all permutations of the digits, which is expensive, we generate all possible powers of two within the integer limit. Since there are only about 30 such values for 32-bit integers, checking each one is efficient and practical.

What is the optimal approach for Reordered Power of 2?

The optimal approach is to compare the digit frequency of the given number with the digit frequency of every power of two within the valid range. If any power of two shares the same digit signature, the digits can be reordered to form it. This avoids generating permutations and keeps the solution efficient.

What data structure is best for solving Reordered Power of 2?

A digit frequency array or hash-based signature works best. It records how many times each digit from 0 to 9 appears in the number. This structure allows quick comparison between the input number and candidate powers of two.

int
countDigits
[
10
]
=
{
0
}
;
7
int
num
=
n
;
8
while
(
num
>
0
)
{
9
countDigits
[
num
%
10
]
++
;
10
num
/=
10
;
11
}
12
13
for
(
int
i
=
0
;
i
<
31
;
++
i
)
{
14
int
powerDigits
[
10
]
=
{
0
}
;
15
int
powerOfTwo
=
1
<<
i
;
16
num
=
powerOfTwo
;
17
while
(
num
>
0
)
{
18
powerDigits
[
num
%
10
]
++
;
19
num
/=
10
;
20
}
21
22
bool match
=
true
;
23
for
(
int
j
=
0
;
j
<
10
;
++
j
)
{
24
if
(
countDigits
[
j
]
!=
powerDigits
[
j
]
)
{
25
match
=
false
;
26
break
;
27
}
28
}
29
30
if
(
match
)
{
31
return
true
;
32
}
33
}
34
35
return
false
;
36
}
37
38
int
main
(
)
{
39
int
n
=
1
;
40
printf
(
"%s\n"
,
reorderedPowerOf2
(
n
)
?
"true"
:
"false"
)
;
41
return
0
;
42
}
/* C does not have a straightforward way to implement permutations visually.
2 Such implementation in C typically requires additional manual handling and
3 recursion, which can get significantly complex and is error-prone in this format.

Explanation

Permutations in C would require explicit handling of stateful recursion and result collections, a task not directly suited for this predefined response format.