forked from srbhr/recursion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintSubSum.java
More file actions
33 lines (23 loc) · 717 Bytes
/
PrintSubSum.java
File metadata and controls
33 lines (23 loc) · 717 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
29
30
31
32
33
import java.util.ArrayList;
public class PrintSubSum {
public static void printSubSum(int indx, int[] arr, int k, ArrayList<Integer> ds, int sum) {
if (indx == arr.length) {
if (sum == k ) {
System.out.println(ds);
}
return;
}
ds.add(arr[indx]);
sum += arr[indx];
printSubSum(indx+1, arr, k, ds, sum);
ds.remove(ds.size()-1);
sum-=arr[indx];
printSubSum(indx+1, arr, k, ds, sum);
}
public static void main(String[] args) {
int[] arr = {3,1,2,4};
int k = 3;
ArrayList<Integer> ds = new ArrayList<>();
printSubSum(0, arr, k, ds, 0);
}
}