Repository navigation
Expand file tree
/
Copy pathCombinationSum.java
More file actions
93 lines (79 loc) · 1.98 KB
/
Copy pathCombinationSum.java
File metadata and controls
93 lines (79 loc) · 1.98 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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
Given an array of integers A and a sum B, find all unique combinations in A where the sum is equal to B.
ach number in A may only be used once in the combination.
Note:
All numbers will be positive integers.
Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
The combinations themselves must be sorted in ascending order.
If there is no combination possible the print "Empty" (without qoutes).
Example,
Given A = 10,1,2,7,6,1,5 and B(sum) 8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
Input:
First is T , no of test cases. 1<=T<=500
Every test case has three lines.
First line is N, size of array. 1<=N<=12
Second line contains N space seperated integers(x). 1<=x<=9.
Third line is the sum B. 1<=B<=30.
Output:
One line per test case, every subset enclosed in () and in every set intergers should be space seperated.(See example)
Example:
Input:
2
7
10 1 2 7 6 1 5
8
5
8 1 8 6 8
12
Output:
(1 1 6)(1 2 5)(1 7)(2 6)
Empty
*/
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class CombinationSum {
public static void main (String[] args) {
//code
Scanner sc = new Scanner(System.in);
int t =sc.nextInt();
while(t-->0)
{
int n = sc.nextInt();
int[] arr = new int[n];
for(int i= 0; i<n; i++)
{
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
int tar = sc.nextInt();
printSubset(arr,tar,0,"",-1);
System.out.println();
}
}
public static void printSubset(int[] arr, int t,int ssf, String ans, int i)
{
if(ssf == t)
{
System.out.print("(");
System.out.print(ans);
System.out.print(")");
return;
}
else if(ssf>t)
{
return;}
else if((i+1)<arr.length){
printSubset(arr,t,ssf+arr[i+1],ans+" "+Integer.toString(arr[i+1]),i+1);
printSubset(arr,t,ssf,ans,i+1);
}
else
return;
}
}