-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathcombinationSum.java
More file actions
41 lines (33 loc) · 1.08 KB
/
combinationSum.java
File metadata and controls
41 lines (33 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.*;
public class combinationSum {
public static void fun(int ind, int[] arr, int tar, List<List<Integer>> res, List<Integer> ds) {
if(ind >= arr.length){
if(tar == 0){
res.add(new ArrayList(ds));
}
return;
}
// not pick
fun(ind+1, arr, tar, res, ds);
// pick
if(arr[ind]<= tar){
ds.add(arr[ind]);
fun(ind, arr, tar-arr[ind], res, ds);
ds.remove(ds.size()-1);
}
}
public static void main(String[] args) {
int[] arr = {2,3,6,7};
int tar = 7;
List<List<Integer>> res = new ArrayList<>();
fun(0, arr, tar, res, new ArrayList<Integer>());
System.out.println(res);
}
}
<<<<<<< HEAD
// This article is Contributed by Himanshu Jha.
// Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/
=======
This article is Contributed by Himanshu Jha.
Connect with me on linkedin https://www.linkedin.com/in/himanshujhaa/
>>>>>>> ac2d3cc604ad9612702aeac09180b1fa93dd5fec