Associated Course(s):
Interview Preparation
Geeks Classes DSA

Given an array of integers A and a sum B, find all unique combinations in A where the sum is equal to B. The same repeated number may be chosen from A unlimited number of times.

**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 = 2,4,6,8 and B(sum) = 8,

A solution set is:

[2, 2, 2, 2]

[2, 2, 4]

[2, 6]

[4, 4]

[8]

**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:**

3

4

7 2 6 5

16

11

6 5 7 1 8 2 9 9 7 7 9

6

4

5 2 2 6

3

**Output:**

(2 2 2 2 2 2 2 2)(2 2 2 2 2 6)(2 2 2 5 5)(2 2 5 7)(2 2 6 6)(2 7 7)(5 5 6)

(1 1 1 1 1 1)(1 1 1 1 2)(1 1 2 2)(1 5)(2 2 2)(6)

Empty

may55 | 86 |

ashujack | 84 |

aman19 | 82 |

abbatta7 | 75 |

lakshmi_pandey | 74 |

saumitra13325 | 648 |

ashujack | 591 |

aman19 | 562 |

lakshmi_pandey | 546 |

aroma17 | 535 |

blackshadows | 5249 |

akhayrutdinov | 5111 |

Ibrahim Nash | 5087 |

Quandray | 4354 |

sanjay05 | 3668 |

Login to report an issue on this page.