Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- We are pushing 0 as the first subarray sum which has no elements.
- then we will take one element of the array and sum in all the previous subsets, for now
- we are having one only, we will do sum of all the elments with 0 seprately and push it into
- the ans array so for arr=[1,2,3] our ans was earlier=[0] and now it becomes ans=[0,0+1,0+2,0+3] then
- we will go to 2 and again we will do sum of 3 with all the elements and push those as the subsets
- so now ans=[0,1,2,3, 1+2, 2+2, 2+3], just keep in mind that new elements should be pushed and need
- not to update the previous elements.
- '''
- class Solution:
- def subsetSums(self, arr):
- ans=[0]
- for i in arr:
- new_sums=[]
- for subset_sum in ans:
- new_sums.append(i+subset_sum)
- ans=ans+new_sums
- return ans
Advertisement
Add Comment
Please, Sign In to add comment