Skip to content

Commit

Permalink
Merge pull request #596 from Anushreebasics/ptch1
Browse files Browse the repository at this point in the history
dy11 q2
  • Loading branch information
kratika-Jangid authored Jan 27, 2024
2 parents 2d21fbe + da05377 commit 35eb903
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Day-11/q2_Combination_sum_II/Anushreebasics--java.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
```
class Solution {
List<List<Integer>> ans;
public void helper(int arr[], int idx, List<Integer> a, int target){
if(target==0){
System.out.println(a);
ans.add(new ArrayList<>(a));
return;
}
if(idx==arr.length) return;
if(arr[idx]<=target){
a.add(arr[idx]);
helper(arr,idx+1,a,target-arr[idx]);
a.remove(a.size()-1);
}
int j = idx+1;
while(j<arr.length && arr[j]==arr[j-1]) j++;
helper(arr,j,a,target);
}
public List<List<Integer>> combinationSum2(int[] arr, int target) {
Arrays.sort(arr);
List<Integer> a = new ArrayList<>();
ans = new ArrayList<>();
helper(arr,0,a,target);
return ans;
}
}
```

0 comments on commit 35eb903

Please sign in to comment.