-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecusionTargetSumSubsets.java
36 lines (29 loc) · 1.07 KB
/
RecusionTargetSumSubsets.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
import java.util.Scanner;
public class RecusionTargetSumSubsets {
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("enter the size of array: ");
int n = scn.nextInt();
System.out.println("enter the value in the array: ");
int[] arr = new int[n];
for(int i = 0; i < arr.length; i++){
arr[i] = scn.nextInt();
}
System.out.println("enter the target: ");
int target = scn.nextInt();
printTSS(arr, 0, "", 0, target);
}
public static void printTSS(int[] arr, int index, String output, int sum, int target){
//base case
if(index == arr.length){
if(sum == target){
System.out.println(output + ".");
}
return;
}
//number part of the subset
printTSS(arr, index + 1, output + arr[index] + ",", sum + arr[index], target);
//number not part of the subset
printTSS(arr, index + 1, output + "", sum + 0, target);
}
}