trying to figure out following problem:
Given a set S of N positive integers the task is to divide them into K subsets such that the sum of the elements values in every of the K subsets is equal.
I want to do this with a set of values not more than 10 integers, with values not bigger than 10 , and less than 5 subsets. All integers need to be distributed, and only perfect solutions (meaning all subsets are equal, no approximations) are accepted. I want to solve it recursively using backtracking. Most ressources I found online were using other approaches I did not understand, using bitmasks or something, or only being for two subsets rather than K subsets.
My first idea was to
- Sort the set by ascending order, check all base cases (e.g. an even distribution is not possible), calculate the average value all subsets have to have so that all subsets are equal.
- Going through each subset, filling each (starting with the biggest values first) until that average value (meaning theyre full) is achieved.
- If the average value for a subset can't be met (undistributed values are too big etc.), go back and try another combination for the previous subset.
- Keep going back if dead ends are encountered.
- stop if all dead ends have been encountered or a perfect solution was found.
Unfortunately I am really struggling with this, especially with implementing the backtrack and retrying new combinations.
Any help is appreciated!