I've m boxes and n balls, each of different size. To give a simple example, let's say we have boxes of size: [10, 50, 20] and balls of size: [20, 10, 30, 10]. It can solved as follows,
Box 0 (Size 10) can store Ball 1 (Size 10) or Ball 3, but let's take Ball 1 for simplicity.
Box 1 (Size 50) can store Ball 0 (size 20) and Ball 2 (size 30)
Box 2 (Size 20) can store Ball 3 (size 10)
To solve this problem, I wrote following is the java code which is solved using sorting and in iterative way,
boolean canBallsFitInBoxes(int [] boxes, int [] balls) {
Arrays.sort(boxes);
Arrays.sort(balls);
int j = 0;
int i = 0;
for(; i < balls.length && j < boxes.length;) {
if(boxes[j] >= balls[i]){
boxes[j] -= balls[i];
i++;
} else {
j++;
}
}
return i == balls.length;
}
I tested for few cases and above iterative solution works. I might have missed edge cases, happy to hear them correct it.
I tried recursive version by myself, but it works only if given arrays are already sorted or when boxes and arrays can be mapped easily like (boxes: [10, 50, 20] and balls: [10, 20, 30, 10]). Basically, when the order of boxes or balls is jumbled, I'm struggling to find a solution using recursion.
But I'm trying to find a generic way to solve this problem. Any suggestions how can I solve it recursively without explicit sorting?