-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path11.Subsets.java
More file actions
72 lines (61 loc) · 1.91 KB
/
11.Subsets.java
File metadata and controls
72 lines (61 loc) · 1.91 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
11. Design and implement in Java to find a subset of a given set S = {Sl, S2,.....,Sn} of n positive
integers whose SUM is equal to a given positive integer d. For example, if S ={1, 2, 5, 6, 8}
and d= 9, there are two solutions {1,2,6}and {1,8}. Display a suitable message, if the given
problem instance doesn't have a solution.
*/
import java.util.Scanner;
class Main {
static int[] arr;
static int count;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter n value");
int n = scanner.nextInt();
arr = new int[n];
System.out.println("Enter Elements of Set");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
System.out.println("Enter Total Sum value");
int total = scanner.nextInt();
subSet(total, n - 1, new boolean[n]);
if (count == 0) {
System.out.println("No solution");
}
}
static void subSet(int total, int index, boolean[] solution) {
if (total == 0) {
printSolution(solution);
} else if (total < 0 || index < 0) {
return;
} else {
boolean[] tempSolution = solution.clone();
tempSolution[index] = false;
subSet(total, index - 1, tempSolution);
tempSolution[index] = true;
subSet(total - arr[index], index - 1, tempSolution);
}
}
static void printSolution(boolean[] solution) {
count += 1;
System.out.print("Solution: ");
for (int i = 0; i < solution.length; i++) {
if (solution[i]) {
System.out.print(arr[i] + " ");
}
}
System.out.println();
}
}
// ///////
// Output:
//
// Enter n value
// 5
// Enter Elements of Set in Increasing Order
// 1 2 5 6 8
// Enter Total Sum value
// 9
// Solution: 1 2 6
// Solution: 1 8