Dsa
Backtracking
Easy
Building Combinations
Given an integer `n`, generate all possible combinations of numbers from 1 to `n` that sum up to a specific value `target`. Each combination must contain unique elements and can be of varying lengths. Print all valid combinations as lists, sorted in ascending order.
Input:
- The first line contains integers `n` (1 ≤ n ≤ 15) and `target` (1 ≤ target ≤ 30).
Output:
- Print the valid combinations as a list of lists.
Example:
Input:
5 5
Output:
[[2, 3], [5]]
Key concepts
backtrackingcombinationsnumbers
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now