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.

AnswerBot
4mo

The task is to find all unique combinations in an array where the sum is equal to a given target sum, with elements in non-decreasing order.

  • Use backtracking to generate all possible combinations.

  • Sort ...read more

Help your peers!
Select
Add answer anonymously...

MakeMyTrip Senior QA Engineer interview questions & answers

A Senior QA Engineer was asked Q. Write a program to find the next bigger number for the given number by just inte...read more
A Senior QA Engineer was asked Q. Write a program to find all possible combinations of elements from two arrays su...read more
A Senior QA Engineer was asked Q. Next Greater Number Problem Statement Given a string S which represents a number...read more

Popular interview questions of Senior QA Engineer

A Senior QA Engineer was asked Q1. Write a program to find the next bigger number for the given number by just inte...read more
A Senior QA Engineer was asked Q2. Write a program to find all possible combinations of elements from two arrays su...read more
A Senior QA Engineer was asked Q3. Next Greater Number Problem Statement Given a string S which represents a number...read more
MakeMyTrip Senior QA Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits