Dsa
Backtracking
Easy
Combination of Ingredients
You are making a dish that requires a combination of ingredients. Given a list of ingredient names and an integer k, output all unique combinations of k ingredients from the list.
Input Format:
- The first line contains an integer n (1 <= n <= 20), the number of ingredients.
- The second line contains n strings representing the names of the ingredients.
- The third line contains an integer k (1 <= k <= n), the number of ingredients to choose.
Output Format:
- Print each combination of ingredients in a new line, sorted alphabetically.
Example:
Input:
4
salt pepper basil oregano
2
Output:
salt basil
salt oregano
pepper basil
pepper oregano
basil oregano
Key concepts
backtrackingcombinatorial generationcooking
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