Associated Course(s):
Must Do Interview Preparation

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

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Sulagna | 117 |

RUTVIKHARIPARA | 116 |

janvidavda105190 | 93 |

harshsiddhapura105461 | 93 |

devsinghindra | 80 |

PiyushPandey4 | 614 |

ASWATHAMA | 561 |

akhyasharma01 | 547 |

john_wick | 521 |

jagrit_07 | 443 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4598 |

Login to report an issue on this page.