Given weights and values of n items and a value k. We need to choose a subset of these items in such a way that ratio of the sum of weight and sum of values of chosen items is K and sum of weight is maximum among all possible subset choices.
Input : weight = [4, 8, 9] values = [2, 4, 6] K = 2 Output : 12 We can choose only first and second item only, because (4 + 8) / (2 + 4) = 2 which is equal to K we can't include third item with weight 9 because then ratio condition won't be satisfied so result will be (4 + 8) = 12
We can solve this problem using dynamic programming. We can make a 2 state dp where dp(i, j) will store maximum possible sum of weights under given conditions when total items are N and required ratio is K.
Now in two states of dp, we will store the last item chosen and the difference between sum of weight and sum of values. We will multiply item values by K so that second state of dp will actually store (sum of weight – K*(sum of values)) for chosen items. Now we can see that our answer will be stored in dp(N-1, 0) because as last item is (N-1)th so all items are being considered and difference between sum of weight and K*(sum of values) is 0 that means sum of weight and sum of values has a ratio K.
After defining above dp state we can write transition among states simply as shown below,
dp(last, diff) = max (dp(last - 1, diff), dp(last-1, diff + wt[last] - val[last]*K)) dp(last – 1, diff) represents the condition when current item is not chosen and dp(last – 1, diff + wt[last] – val[last] * K)) represents the condition when current item is chosen so difference is updated with weight and value of current item.
In below code a top-down approach is used for solving this dynamic programming and for storing dp states a map is used because the difference can be negative also and the 2D array can create problem in that case and special care need to be taken.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Count number of paths whose weight is exactly X and has at-least one edge of weight M
- Maximum weight transformation of a given string
- Maximum weight path ending at any element of last row in a matrix
- Number of Paths of Weight W in a K-ary tree
- Minimum cost to fill given weight in a bag
- Number of ways to choose elements from the array such that their average is K
- Maximum sum such that no two elements are adjacent | Set 2
- Size of The Subarray With Maximum Sum
- Maximum Sum Subsequence of length k
- Maximum subarray sum in O(n) using prefix sum
- Maximum Sum Decreasing Subsequence
- Maximum sum bitonic subarray
- Maximum length of segments of 0's and 1's
- Longest subarray having maximum sum
- Maximum subsequence sum such that no three are consecutive
Improved By : vibhu4agarwal