Given an integer **N**, the task is to find the numbers which when added after being raised to the Power of 2 gives the integer **N**.

**Examples:**

Input:N = 12345

Output:0, 3, 4, 5, 12, 13

Explanation:

12345 = 2^0 + 2^3 + 2^4 + 2^5 + 2^12 + 2^13

Input:N = 10000

Output:4, 8, 9, 10, 13

Explanation:

10000 = 2^4 + 2^8 + 2^9 + 2^10 + 2^13

**Approach:**

- Since every number can be expressed as sum of powers of 2, the task is to print every
**i**when i^{th}bit is set in the binary representation of**N**. -
**Illustration:**

(29)_{10}= (11101)_{2}

Thus, in 29, {0, 2, 3, 4} are the indices of the set bits.

2^{0}+ 2^{2}+ 2^{3}+ 2^{4}

= 1 + 4 + 8 + 16

= 29 - In order to check if i
^{th}bit is set, we only need to check:

if( N & (1 << i) ) == 1

**Illustration:**

(29)_{10}= (11101)_{2}

0^{th}bit: 11101 & 1 = 1

1^{st}bit: 11101 & 10 = 0

2^{nd}bit: 11101 & 100 = 1

3^{rd}bit: 11101 & 1000 = 1

4^{th}bit: 11101 & 10000 = 1

Below is the implementation of the above approach.

## C++

`// C++ Program to split N ` `// into numbers which when ` `// added after being raised ` `// to the power of 2 gives N ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to print the numbers ` `// which raised to power of 2 ` `// adds up to N ` `void` `PowerOfTwo(` `long` `long` `N) ` `{ ` ` ` `for` `(` `int` `i = 0; i < 64; i++) { ` ` ` `long` `long` `x = 1; ` ` ` ` ` `// Checking if i-th bit is ` ` ` `// set in N or not by ` ` ` `// shifting 1 i bits to ` ` ` `// the left and performing ` ` ` `// AND with N ` ` ` `if` `(N & (x << i)) ` ` ` `cout << i << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `long` `long` `N = 12345; ` ` ` `PowerOfTwo(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to split N ` `// into numbers which when ` `// added after being raised ` `// to the power of 2 gives N ` `class` `GFG{ ` ` ` `// Function to print the numbers ` `// which raised to power of 2 ` `// adds up to N ` `static` `void` `PowerOfTwo(` `long` `N) ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `64` `; i++) ` ` ` `{ ` ` ` `long` `x = ` `1` `; ` ` ` ` ` `// Checking if i-th bit is ` ` ` `// set in N or not by ` ` ` `// shifting 1 i bits to ` ` ` `// the left and performing ` ` ` `// AND with N ` ` ` `if` `((N & (x << i)) > ` `0` `) ` ` ` `System.out.print(i + ` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `long` `N = ` `12345` `; ` ` ` `PowerOfTwo(N); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to split N ` `# into numbers which when ` `# added after being raised ` `# to the power of 2 gives N ` ` ` `# Function to print the numbers ` `# which raised to power of 2 ` `# adds up to N ` `def` `PowerOfTwo(N): ` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, ` `64` `): ` ` ` `x ` `=` `1` ` ` ` ` `# Checking if i-th bit is ` ` ` `# set in N or not by ` ` ` `# shifting 1 i bits to ` ` ` `# the left and performing ` ` ` `# AND with N ` ` ` `if` `(N & (x << i)) > ` `0` `: ` ` ` `print` `(i, end ` `=` `" "` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# Given number ` ` ` `N ` `=` `12345` `; ` ` ` ` ` `# Function call ` ` ` `PowerOfTwo(N) ` ` ` `# This code is contributed by rock_cool ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to split N ` `// into numbers which when ` `// added after being raised ` `// to the power of 2 gives N ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to print the numbers ` `// which raised to power of 2 ` `// adds up to N ` `static` `void` `PowerOfTwo(` `long` `N) ` `{ ` ` ` `for` `(` `int` `i = 0; i < 64; i++) ` ` ` `{ ` ` ` `long` `x = 1; ` ` ` ` ` `// Checking if i-th bit is ` ` ` `// set in N or not by ` ` ` `// shifting 1 i bits to ` ` ` `// the left and performing ` ` ` `// AND with N ` ` ` `if` `((N & (x << i)) > 0) ` ` ` `Console.Write(i + ` `" "` `); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `long` `N = 12345; ` ` ` `PowerOfTwo(N); ` `} ` `} ` ` ` `// This code is contributed by Nidhi_biet ` |

*chevron_right*

*filter_none*

**Output:**

0 3 4 5 12 13

**Time Complexity:** O(log N)

**Space Complexity:** O(1)

For an alternative approach, refer to Powers of 2 to required sum

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find a Mother vertex in a Graph using Bit Masking
- Powers of 2 to required sum
- Minimum number of given powers of 2 required to represent a number
- Inserting m into n such that m starts at bit j and ends at bit i.
- Inserting M into N such that m starts at bit j and ends at bit i | Set-2
- Minimum bit flips such that every K consecutive bits contain at least one set bit
- Find k numbers which are powers of 2 and have sum N | Set 1
- Sum of first N natural numbers by taking powers of 2 as negative number
- Sum of first N natural numbers which are not powers of K
- Represent n as the sum of exactly k powers of two | Set 2
- Check if a number can be represented as sum of non zero powers of 2
- Distinct powers of a number N such that the sum is equal to K
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Sum of first N natural numbers with all powers of 2 added twice
- Check if a number can be expressed as sum of two Perfect powers
- Nth Subset of the Sequence consisting of powers of K in increasing order of their Sum
- Print all integers that are sum of powers of two given numbers
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Count number of steps to cover a distance if steps can be taken in powers of 2
- Number of triangles possible with given lengths of sticks which are powers of 2

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.