Ad
  • Default User Avatar

    GISTS list is very particular: each key is a power of 2.

    2^0 = 1, 2^1 = 2, 2^2 = 4, 2^3 = 8...
    

    If you think in binary representation, each key is tied to one particular bit, then each gift corresponds to this very bit.

    1 =   0b00000000001
    2 =   0b00000000010
    ...
    256 = 0b01000000000
    512 = 0b10000000000
    

    Take the binary representation of a number n and look at each of its turned on bit (1).
    It gives you a list of gifts as one bit matches one gift.

    n = 35 = 0b0000010011 = 0b0000010000 + 0b0000000010 + 0b0000000001 = 32 + 2 + 1
    answer = (GIFTS[32], GIFTS[2], GIFTS[1])
    

    Finally all you need to do is to sort the answer to output an alphabetically sorted list.

    Is it more understandable now?