Sum of Set

By | December 3, 2015
Share the joy
  •  
  •  
  •  
  •  
  •  
  •  

Given a integer array, and a number n. Return true if n can be consisted by a subset of array.
For example, arr[]={3, 2, 3, 5}, n=11, return true; arr[]={3, 2, 3, 5}, n=12, return false

This problem can be solved by HashSet. Loop element i in arr[]. For each loop, get the value of sum i and each element in hashset. And put the value in hashset again. And put element i in hashset at the end of loop.

Check my code on github: link