So, you want to generate all possible subsets of a set of numbers and list their sum.
First you want to list all the subsets. Since the set containing the elements of N contains 2 ^ N subsets, you can simply do this by iterating through natural numbers from 0 to 1 << (sizeof(arr) / sizeof(arr[0])) and processing the binary representation of the number as follows way: if a specific bit is set at position k , then the element k th is in the currently created subset, otherwise it is not.
Then you must add all the selected elements together and save the result in the next slot of another array (size 2 ^ N , obviously).
user529758
source share