• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
July 07, 2022 |9.9K Views

Powers of 2 to required sum

  Share   Like
Description
Discussion

Every number can be described in powers of 2. 
Example : 29 = 2^0 + 2^2 + 2^3 + 2^4.
2^0 ( exponent of 2 is ‘0’) 0 
2^2 ( exponent of 2 is ‘2’) 1 
2^3 ( exponent of 2 is ‘3’) 3 
2^4 ( exponent of 2 is ‘4’) 4
Convert each number into its binary equivalent by pushing remainder of given number, when divided by 2 till it is greater than 0, to vector. Now, Iterate through its binary equivalent and whenever there is set bit, just print the i-th value(iteration number).


Powers of 2 to required sum : https://www.geeksforgeeks.org/powers-2-required-sum/