Update appNew update is available. Click here to update.

Combination Sum III

Contributed by
Akhilesh Singh Bhadauria
Last Updated: 23 Feb, 2023
Medium
yellow-spark
0/80
Share
5 upvotes

Problem Statement

You are given ‘K’ and ‘N’ and you have to do the following:-

Output all possible combinations of arrays whose elements sum is equal to ‘N’ and you can use only elements in the range 1 to 9 inclusive and you can use each element only once and the size of the combination should be ‘K’.

Two combinations are called different if there is an element that is in one combination and not in another.

Example:

Input: ‘K’ = 2, ‘N’ = ‘5’

Output: [[1, 4], [2, 3]]

1 + 4 = 5 and 2 + 3 = 5. Only these two combinations are there which sum upto n so answer is [[1, 4], [2, 3]].
Detailed explanation ( Input/output format, Notes, Images )
Sample Input 1 :
2
2
5
3
8
Sample Output 1 :
1 4
2 3
1 2 5
1 3 4
Explanation Of Sample Input 1 :
For the first case:
1 + 4 = 5 and 2 + 3 = 5. Only these two combinations are there which sum upto n so answer is [[1, 4], [2, 3]].

For the second case:
1 + 2 + 5 = 8 and 1 + 3 + 4 = 8. Only these two combinations are there which sum upto n so answer is [[1, 2, 5], [1, 3, 4]].
Sample Input 2 :
2
4
10
3
9
Sample Output 2 :
1 2 3 4
1 2 6
1 3 5
2 3 4
Reset Code
Full screen
Auto
copy-code
Console