-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ15.java
More file actions
28 lines (22 loc) · 665 Bytes
/
Q15.java
File metadata and controls
28 lines (22 loc) · 665 Bytes
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
import java.util.ArrayList;
public class Q15 {
public static void main(String[] args) {
ArrayList<Object> ans= new ArrayList<> ();
sum(new int[]{1 , 1 , 1 , 2, 2} , ans, 4 , 0);
}
private static void sum(int[] a,ArrayList<Object> ans, int tar, int idx) {
if (idx >= a.length ){
if(tar == 0 ){
System.out.println (ans);
return;
}
return;
}
if(tar >= a[idx]){
ans.add (a[idx]);
sum( a, ans, tar-a[idx], idx +1);
ans.remove (ans.size () -1 );
sum( a, ans, tar, idx +1);
}
}
}