
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Reordered Power of 2 in C++
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 number is like 46, then the answer will be true.
To solve this, we will follow these steps −
Define a method called count, this will take x as input
ret := 0
while x is not 0
ret := ret + 10 ^ last digit of x
x := x / 10
return ret
From the main method do the following −
x := count(N)
for i in range 0 to 31
if count(2^i) = x, then return true
return false
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int count(int x){ int ret = 0; while(x){ ret += pow(10, x % 10); x /= 10; } return ret; } bool reorderedPowerOf2(int N) { int x = count(N); for(int i = 0; i < 32; i++){ if(count(1 << i) == x) return true; } return false; } }; main(){ Solution ob; cout << (ob.reorderedPowerOf2(812)); }
Input
812
Output
1
- Related Articles
- Program to find Reordered Power of 2 in Python
- Find the highest power of 2 in 1440.
- Checking power of 2 using bitwise operations in JavaScript
- Nearest power 2 of a number - JavaScript
- Check if given number is a power of d where d is a power of 2 in Python
- Number of digits in 2 raised to power n in C++
- C/C++ Program to Find sum of Series with n-th term as n power of 2 - (n-1) power of 2
- Number of pairs whose sum is a power of 2 in C++
- Compute modulus division by a power-of-2-number in C#
- Find the value of:2 to the power 6.
- Program to find out the value of a power of 2 in Python
- Find value of y mod (2 raised to power x) in C++
- What is 2 to the power 100?
- Power in AC Circuit – Active Power, Reactive Power, Apparent Power
- How to check if a number is a power of 2 in C#?

Advertisements