Finding the sum of all subsets of a given set

前端 未结 3 778
时光取名叫无心
时光取名叫无心 2020-12-22 15:18

Suggest an algorithm for finding the sum of all subsets of a set.

For example, if k=3 and the subsets are {1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}

3条回答
  •  攒了一身酷
    2020-12-22 16:03

    Each element appears the same number of times, which happens to be 2n-1 where n is number of elements. So the answer is: count sum of elements in the set and multiply it by 2n-1

提交回复
热议问题