-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsm.java
More file actions
29 lines (26 loc) · 910 Bytes
/
Copy pathtsm.java
File metadata and controls
29 lines (26 loc) · 910 Bytes
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
import java.util.Scanner;
public class tsm {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int[]arr = new int[n];
for(int i = 0 ; i< arr.length ; i++){
arr[i] = scn.nextInt();
}
int tar = scn.nextInt();
printTargetSumSubsets(arr , 0 , "" , 0 ,tar);
}
public static void printTargetSumSubsets(int[] arr , int idx , String set , int sos , int tar){
if(sos < tar){//agar sum of subset chhota hai target set se return kar de
return;
}
if(idx == arr.length){
if(sos == tar){
System.out.println(set + ".");
}
return;
}
printTargetSumSubsets(arr , idx +1 , set + arr[idx] + ", " ,sos + arr[idx] ,tar);
printTargetSumSubsets(arr , idx +1 , set , sos , tar);
}
}