-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsetSum.java
96 lines (84 loc) · 3.41 KB
/
subsetSum.java
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package Backtracking;
public class subsetSum {
public static void main(String[] args) {
int[] input = {10, 7, 5, 18, 12, 20, 15};
int targetSum = 45;
subsetSum subSetSum = new subsetSum();
subSetSum.findSubSets(input, targetSum);
}
private int[] set;
private int[] selectedElements;
private int targetSum;
private int numOfElements;
public void findSubSets(int[] set, int targetSum) {
this.set = set;
this.numOfElements = set.length;
this.targetSum = targetSum;
selectedElements = new int[numOfElements];
quicksort(set, 0, numOfElements-1);
int sumOfAllElements = 0;
for(int element : set){
sumOfAllElements += element;
}
findSubSets(0, 0, sumOfAllElements);
}
private void findSubSets(int sumTillNow, int index, int sumOfRemaining) {
selectedElements[index] = 1; // selecting element at index : 'index'
if (targetSum == set[index] + sumTillNow) {
print();
}
// (sum + set[index] + set[index+1] <= targetSum) : this condition
// ensures selecting
// the next element is useful and the total sum by including next
// element will not exceed the target sum.
if ((index + 1 < numOfElements) && (sumTillNow + set[index] + set[index + 1] <= targetSum)) {
findSubSets(sumTillNow + set[index], index + 1, sumOfRemaining - set[index]);
}
// now exploring the other path: not Selecting the element at index:
// 'index'
selectedElements[index] = 0;
// (sum + set[index+1] <= targetSum) : this condition ensures selecting
// the next element is useful and the total sum by including next
// element will not exceed the target sum.
// (sum + sumOfRemaining - set[index] >= targetSum) ensures the total
// sum of all the elements by excluding the current element may achieve
// the target sum, if in case the resultant sum is less than the target
// sum then exploring this path is of no use
if ((index + 1 < numOfElements) && (sumTillNow + set[index + 1] <= targetSum)
&& (sumTillNow + sumOfRemaining - set[index] >= targetSum)) {
findSubSets(sumTillNow, index + 1, sumOfRemaining - set[index]);
}
}
private void print() {
for (int i = 0; i < numOfElements; i++) {
if (selectedElements[i] == 1) {
System.out.print(set[i]+" ");
}
}
System.out.println();
}
private void quicksort(int[] arr, int start, int end) {
if (start < end) {
swap(arr, (start + (end - start) / 2), end);
int pIndex = partition(arr, start, end);
quicksort(arr, start, pIndex - 1);
quicksort(arr, pIndex + 1, end);
}
}
private int partition(int[] arr, int start, int end) {
int pIndex = start, pivot = arr[end];
for (int i = start; i < end; i++) {
if (arr[i] < pivot) {
swap(arr,pIndex,i);
pIndex++;
}
}
swap(arr,pIndex,end);
return pIndex;
}
private void swap(int[] arr, int index1, int index2) {
int temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
}