-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintSubsetSumToK.java
More file actions
55 lines (46 loc) · 1.42 KB
/
PrintSubsetSumToK.java
File metadata and controls
55 lines (46 loc) · 1.42 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Print Subset Sum to K
// Hard
// Score
// 0/600
// Average time to solve is 60m
// Problem statement
// Given an array A and an integer K, print all subsets of A which sum to K.
// Subsets are of length varying from 0 to n, that contain elements of the
// array. But the order of elements should remain same as in the input array.
// Note :
// The order of subsets are not important. Just print them in different lines.
// Detailed explanation ( Input/output format, Notes, Images )
// Sample Input:
// 9
// 5 12 3 17 1 18 15 3 17
// 6
// Sample Output:
// 3 3
// 5 1
import java.util.ArrayList;
public class solution {
public static void printSubsetsSumTok(int input[], int k) {
// Write your code here
ArrayList<Integer> arr = new ArrayList<>();
helper(input, k, 0, arr, 0);
}
public static void helper(int input[], int k, int index, ArrayList<Integer> arr, int sum) {
if (index == input.length) {
if (sum == k) {
for (int i = 0; i < arr.size(); i++) {
System.out.print(arr.get(i) + " ");
}
System.out.println();
}
return;
}
// Take
sum += input[index];
arr.add(input[index]);
helper(input, k, index + 1, arr, sum);
// Not take
sum -= input[index];
arr.remove(arr.size() - 1);
helper(input, k, index + 1, arr, sum);
}
}