-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNonContiguousSubArraySum.java
More file actions
38 lines (31 loc) · 1.07 KB
/
NonContiguousSubArraySum.java
File metadata and controls
38 lines (31 loc) · 1.07 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
package Algorithms.BackTracking;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 06 Oct 2024
*/
public class NonContiguousSubArraySum {
public static void main(String[] args) {
int[] arr = {14,9,8,4,3,2};
int k = Arrays.stream(arr).sum()/2;
System.out.println(backtrack(arr, k, 0, 0, new HashMap<>()));
}
/**
* TLE in LeetCode
*/
private static boolean backtrack(int[] nums, int sum, int index, int currentSum, Map<String, Boolean> memo) {
if (currentSum == sum)
return true;
if (index >= nums.length || currentSum > sum)
return false;
String key = index + "," + currentSum;
if (memo.containsKey(key))
return memo.get(key);
boolean include = backtrack(nums, sum, index + 1, currentSum + nums[index], memo);
boolean exclude = backtrack(nums, sum, index + 1, currentSum, memo);
memo.put(key, include || exclude);
return include || exclude;
}
}