Folks,
I tried the code for k-size subset of an array. It prints the array subset, but doesn't show up the pairs like [1,2] and [2,1].
Below is my code :-
class KSizeSubSetArray {
private static void getSubsets(List<Integer> superSet, int k, int idx, Set<Integer> current,List<Set<Integer>> solution) {
if (current.size() == k) {
solution.add(new HashSet<Integer>(current));
return;
}
if (idx == superSet.size())
return;
Integer x = superSet.get(idx);
current.add(x);
getSubsets(superSet, k, idx+1, current, solution);
current.remove(x);
getSubsets(superSet, k, idx+1, current, solution);
}
public static List<Set<Integer>> getSubsets(List<Integer> superSet, int k) {
List<Set<Integer>> res = new ArrayList<Set<Integer>>();
getSubsets(superSet, k, 0, new HashSet<Integer>(), res);
return res;
}
public static void main(String[] args) {
List<Integer> superSet = new ArrayList<Integer>();
superSet.add(1);
superSet.add(2);
superSet.add(3);
superSet.add(4);
System.out.println(getSubsets(superSet,2));
}
}
The above code prints the output :-
[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
But I'm expecting to print both pairs like [1,2] and [2,1].
Am I missing something ?