-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroupSum.java
72 lines (64 loc) · 1.76 KB
/
GroupSum.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
import java.util.Arrays;
public class GroupSum {
/**
* Group sum with subtraction
*
* @param nums
* @param target
* @return
*/
public boolean groupSum(int[] nums, int target) {
return groupSumRec(nums, target, 0);
}
/**
* Group sum by summing up
*
* @param nums
* @param target
* @return
*/
public boolean groupSum2(int[] nums, int target) {
return groupSumRec2(nums, target, 0, 0);
}
/**
* Find the Power Set of the given numbers
*
* @param nums
*/
public void powerSet(int[] nums) {
powerSetRec(nums, 0, new int[] {});
}
private boolean groupSumRec(int[] nums, int target, int i) {
if (i == nums.length) {
return 0 == target;
}
/*
* Two cases: 1. Include current number (nums[i]) 2. Do NOT include current
* number (nums[i])
*/
return groupSumRec(nums, target - nums[i], i + 1)
|| groupSumRec(nums, target, i + 1);
}
private boolean groupSumRec2(int[] nums, int target, int i, int sum) {
if (i == nums.length) {
return sum == target;
}
// Same 2 scenarios: add current number or not
return groupSumRec2(nums, target, i + 1, sum + nums[i])
|| groupSumRec2(nums, target, i + 1, sum);
}
private void powerSetRec(int[] nums, int i, int[] currentElement) {
if (i == nums.length) {
System.out.println(Arrays.toString(currentElement));
return;
}
// Create new array as array is object and lives in Java Heap shared memory
int[] withCurrentNumber = new int[currentElement.length + 1];
System.arraycopy(currentElement, 0, withCurrentNumber, 0, currentElement.length);
withCurrentNumber[withCurrentNumber.length - 1] = nums[i];
// case 1: With current number
powerSetRec(nums, i + 1, withCurrentNumber);
// case 2: Without current number
powerSetRec(nums, i + 1, currentElement);
}
}