Dsa
Backtracking
Medium
Group Generations
You are tasked with creating a function that generates all the possible ways to divide a set of N unique items into groups of exactly K items. If the number of items is not divisible by K, return an empty list.
Input format:
- The first line contains an integer N (1 <= N <= 15), the total number of unique items.
- The second line contains an integer K (1 <= K <= N), the size of each group.
Output format:
- Print all possible groupings as a list of lists. If no groupings are possible, print an empty list.
Example:
Input:
4
2
Output:
[[1, 2], [3, 4]], [[1, 3], [2, 4]], [[1, 4], [2, 3]]
Key concepts
backtrackingcombinatorialrecursion
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