tobias_tris's blog

By tobias_tris, history, 3 years ago, In English

Determine whether it is possible to choose X numbers that are powers of 2 and add up to N.

  • Vote: I like it
  • +6
  • Vote: I do not like it

| Write comment?
»
3 years ago, hide # |
 
Vote: I like it -7 Vote: I do not like it

You can check how many bits are set in the number N. if its equal to x then it is possible

  • »
    »
    3 years ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    I don't think it will work. let's take N = 9 and X = 3. The answer according to you is false, but (4, 4, 1) will work.

»
3 years ago, hide # |
← Rev. 4  
Vote: I like it 0 Vote: I do not like it

Answer is yes iff sbits(N) is <=X and X<=N.

sbits(x) refers to the number of 1s in the binary representation of x

Proof