Given an array of integers a
, two numbers N
and M
, return N
group of integers from a
such that each group sums to M
.
For example, say:
a = [1,2,3,4,5]
N = 2
M = 5
Then the algorithm could return [2, 3], [1, 4]
or [5], [2, 3]
or possibly others.
What algorithms could I use here?
Edit:
I wasn't aware that this problem is NP complete. So maybe it would help if I provided more details on my specific scenario:
So I'm trying to create a "match-up" application. Given the number of teams N
and the number of players per team M
, the application listens for client requests. Each client request will give a number of players that the client represents. So if I need 2 teams of 5 players, then if 5 clients send requests, each representing 1, 2, 3, 4, 5 players respectively, then my application should generate a match-up between clients [1, 4]
and clients [2, 3]
. It could also generate a match-up between [1, 4]
and [5]
; I don't really care.
One implication is that any client representing more than M
or less than 0
players is invalid. Hope this could simplify the problem.