how to find unique pair of sums which add to n? I have done backtracking easy way, but its showing duplicates? How to find unique pairs
[1,1,2,2,3,3,], n = 6
ans = [1,2,3], [3,3], [1,1,2,2,]
current output
[3, 3]
[3, 2, 1]
[3, 2, 1]
[3, 2, 1]
[3, 2, 1]
[3, 2, 1]
[3, 2, 1]
[3, 2, 1]
[3, 2, 1]
[2, 2, 1, 1]
Please no sets allowed
public List<List<Integer>> findPairs(int[] arr, int n) {
List<List<Integer>> result = new ArrayList<>();
if(sum == 0) {
List<Integer> ans = new ArrayList<>();
return Collections.singletonList(ans);
}
else if(sum < 0 || idx == arr.length) {
return null;
}
else {
List<List<Integer>> r1 = findPairSum(arr, idx + 1, sum);
if(r1 != null) {
result.addAll(r1);
}
List<List<Integer>> r2 = findPairSum(arr, idx + 1, sum - arr[idx]);
if(r2 != null) {
for(List<Integer> x : r2) {
x.add(arr[idx]);
}
result.addAll(r2);
}
}
return result;
}