Combination Sum Problem Statement

Given an array of distinct positive integers ARR and a non-negative integer 'B', find all unique combinations in the array where the sum is equal to 'B'. Numbers can be chosen multiple times from ARR. Ensure that elements in each combination are presented in non-decreasing order.

Input:

The first line contains an integer 'T', the number of test cases.
Each test case consists of:
- An integer 'N', the number of elements in the array, and an integer 'B', the target sum, on the first line.
- An array of 'N' space-separated integers on the second line.

Output:

For each test case, output all possible combinations that sum up to 'B'. Each combination should be printed on a separate line. Combinations should be in non-decreasing order, but they can be printed in any sequence.

Example:

Input:
ARR = [1, 2, 3], B = 5
Output:
(1, 1, 1, 1, 1)
(1, 1, 1, 2)
(1, 1, 3)
(1, 2, 2)
(2, 3)

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 15
  • 1 <= B <= 20
  • 1 <= ARR[i] <= 20

Time Limit: 1 second

Note:

There is no need to print the output; you only need to implement the solution in the function provided.

Be the first one to answer
Add answer anonymously...
MakeMyTrip Senior QA Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter