0

Find All Subsets from Given Set For Target Value.

I’m having trouble implementing the getSubset function to find the target value from the user input. Can anyone can help?

18th Jan 2019, 5:05 AM
Edwin Sherman
Edwin Sherman - avatar
2 ответов
+ 2
Can you share your code here?
18th Jan 2019, 12:36 PM
Seniru
Seniru - avatar
0
static ArrayList<ArrayList<Integer>> getSubset(Integer[] set, int index){ ArrayList<ArrayList<Integer>> allSubsets; if( index < 0){ allSubsets = new ArrayList<>(); allSubsets.add(new ArrayList<>(); } else { all Subsets = getSubset(set, index -1); Integer item = set[index]; ArrayList<ArrayList<Integer>> moreSubsets = new ArrayList<>(); allSubsets.stream().map((ArrayList<Integer> subset) -> { ArrayList<Integer> newSubset = new ArrayList<>(); newSubset.addAll(subset); return newSubset; }) .map((newSubset) ->{ newSubset.add(item); return newSubset; }) .forEachOrdered((newSubset) -> { moreSubsets.add(newSubsets); }); allSubsets.addAll(moreSubsets); } return allSubsets; } ... ... And once I ask the user for the target input im trying to call that function to retrieve all possible subset for target value.
18th Jan 2019, 3:25 PM
Edwin Sherman
Edwin Sherman - avatar