At a career fair, I was asked the following tough question(not exactly as below, I stripped out the story and expressed the problem (more or less) formally).
Given number
K, and a finite list of pairsL = < (a,b), (c,d), (e,f) >where each pairpconsists of two numbersn1andn2. Find the the listRwhere the sum of then1values of all its pairs is the greatest and the sum of all then2values of its pairs is less than or equal toK. In the listR, pairs can repeat.
So for example, if your K is 10 and you have the list of pairs L as <(3,2) , (1,7), (4,6) >, then the result would be R = < (3,2), (3,2), (3,2), (3,2), (3,2) > so that the sum of all n1 values would be 3 + 3 + 3 + 3 + 3 = 15 and sum of all the n2 values would be 2 + 2 + 2 + 2 + 2 = 10. This would be correct solution as opposed to something like <(3,2), (3,2), (4,6)> (n1 sum is 10; n2 sum is 10) or like <(1,7), (3,2)> (n1 sum is 4; n2 sum is 9) whose n1 sums are not the maximum possible.
I described an approach where I would essentially enumerate all possible combinations of pairs whose n2 values would sum to number less than or equal to K and pick the combination with highest n1 sum. Enumeration could be done by incrementally subtracting from K, each of n2 values from pairs in the given list L.
Is there a better way to do this ?