-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_sum.java
53 lines (40 loc) · 1.29 KB
/
target_sum.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
import java.util.*;
class Result {
public static int findTargetSumWays(List<Integer> arr, int tgt) {
int n = arr.size();
int count = 0;
int totalCombinations = 1 << n;
for (int mask = 0; mask < totalCombinations; mask++) {
int sum = 0;
for (int i = 0; i < n; i++) {
if ((mask & (1 << i)) != 0) {
sum += arr.get(i);
} else {
sum -= arr.get(i);
}
}
if (sum == tgt) {
count++;
}
}
return count;
}
}
public class target_sum {
public static void main(String[] args) {
Result r = new Result();
Scanner sc = new Scanner(System.in);
// Input the array
ArrayList<Integer> arr = new ArrayList<>();
System.out.println("Enter array elements (end with -1):");
while (true) {
int num = sc.nextInt();
if (num == -1) break; // Sentinel value to end input
arr.add(num);
}
System.out.println("Enter target sum:");
int target = sc.nextInt();
// Call the method and print the result
System.out.println(r.findTargetSumWays(arr, target));
}
}