You are given 'N' apples and multiple boxes with capacities 'A', 'B' and 'C'. Now, you need to calculate the maximum number of boxes in which you can distribute these apples such that no apple is left and none of the boxes is partially filled.
Example:Suppose, you have 7 apples and multiple boxes of capacities 2, 3 and 5.
You can keep these 7 apples in maximum 3 boxes i.e. two boxes of capacity 2 and one box of capacity 3.
The first line of input contains an integer 'T' denoting the number of test cases.
The first line of each test case contains a single integer 'N' denoting the number of apples.
The second line of each test case contains three space-separated integers 'A', 'B', and 'C' denoting the capacity of three types of boxes.
Output format:
For each test case, return the maximum number of boxes in which you can distribute these 'N' apples.
Print the output of each test case in a separate line.
Note:
1. If it is impossible to distribute the apples into the boxes, simply return 0.
2. You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 10 ^ 5
1 <= A, B, C <= 10 ^ 4
Time limit: 1 sec
2
5
2 3 5
11
2 3 7
2
5
In test case 1, There are 2 ways to distribute 5 apples in boxes with capacity 2, 3 and 5.
2 apples in one box and 3 apples in another box, boxes used: 2
5 apples in 1 box, box used: 1
Maximum boxes are used in the first way, thus, the answer is 2.
In test case 2, There are 3 ways to distribute 11 apples in boxes with capacity 2, 3 and 7.
2 apples in four boxes (2 * 4) and 3 apples in 1 box (3 * 1), boxes used: 5
2 apples in 1 box (2 * 1) and 3 apples in 3 boxes( 3 * 3), boxes used: 4
2 apples in 2 boxes (2 * 2) and 7 apples in 1 box (7 * 1), boxes used: 3
Maximum boxes are used in the first way, thus, the answer is 5.
2
4
2 1 1
8
5 6 7
4
0
In test case 1, There are 4 apples and boxes with capacity 2, 3 and 5.
Maximum boxes will be used when all the apples are in boxes with capacity 1 and thus maximum boxes used are 4.
In test case 2, There is no way to distribute 8 apples in the boxes with capacity 5, 6 and 7.
The very first approach can be to try all possible distributions of the apples and maximize the ans among the valid ones.
O(3 ^ H), where ‘H’ is the maximum height of the recursive tree.
In the worst case, during each recursive call, the value of N decreases either by ‘A’, ‘B’, or ‘C’. i.e. T(N) = T(N - A) + T(N - B) + T(N - C). Now, the maximum height is reached when the decrease in N is minimum. i.e. N decreases by min(A, B, C).
O('N' / min('A', ‘B’, ‘C’) , Where ‘N’ is the quantity of apples and ‘A’, ‘B’, ‘C’ are the quantity of boxes.
Since extra space will be used by the recursion stack which can go to max depth of N / min(A, B, C).