Problem 1843. Optimally fill a container with elements of different sizes
Given a row vector V of different numerical values and a size limit L, return a 1x2 vector A, containing (a) the largest sum of any subset of elements in the input vector V, which is lower than or equal to the size limit, and (b) the number of different possible subsets of V with which this sum can be obtained.
Note that two or more elements in V may have the same value and will be considered as two different possibilities.
Example 1:
Input V = [ 4 2 6 3 6 ]
Input L = 11
Output A = [ 11 2 ]
The size limit of 11 can be obtained (exactly, in this case) by adding 2, 3 and 6 together, but there are two different elements with the value of 6 and, therefore, two possible subsets of V satisfying the condition.
Example 2:
Input V = [ 1.1 1.3 0.6 0.9 ]
Input L = 2.5
Output A = [ 2.4 1 ]
The sum of 2.4, which is the highest sum of any subset of V that does not exceed L, can be obtained in only one way.
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers22
Suggested Problems
-
Sort a list of complex numbers based on far they are from the origin.
5727 Solvers
-
Find the alphabetic word product
3396 Solvers
-
1545 Solvers
-
532 Solvers
-
Let's get back to school, and create multiplication tables
223 Solvers
More from this Author45
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!