I’m looking for an efficient way to assign n people to m rooms in a very specific way.

INPUT:

The program receives two sets of people (set of males and set of females), as well as a set of available rooms (rooms can have different sizes, ranging from 2 to 6).

ASSIGNMENT CONDITIONS:

The algorithm must assign people to the rooms in a way, that:

- Males can be in a room with only males (same for females)
- Noone can be alone
- The algorithm must maximize groups (one room of 4 is preferred over 2 rooms of 2, etc)

OUTPUT:

The algorithm must return the assignment

I’ve tried to do it, but all of the sollutions I come up with are at least O(n^3). Does anyone know an efficient way to do this?