Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Combination Sum III
MEDIUM
3 upvotes
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Combination Sum III

Contributed by
Akhilesh Singh Bhadauria
Medium
yellow-spark
0/80
Share
3 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, Constraints, 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