![]() |
VOOZH | about |
Given two arrays, val[] and wt[], representing the values and weights of item respectively, and an integer capacity representing the maximum weight a knapsack can hold, we have to determine the maximum total value that can be achieved by putting the items in the knapsack without exceeding its capacity.
Items can also be taken in fractional parts if required.
Examples:
Input: val[] = [60, 100, 120], wt[] = [10, 20, 30], capacity = 50
Output: 240
Explanation: We will take the items of weight 10kg and 20kg and 2/3 fraction of 30kg.
Hence total value will be 60 + 100 + (2/3) * 120 = 240.Input: val[] = [500], wt[] = [30], capacity = 10
Output: 166.667
Case 1: Picking the items with smaller weights first
Example: val[] = [10, 10, 10, 100], wt[] = [10, 10, 10, 30], capacity = 30
So, choosing smaller weights first fails here.
Case 2: Picking items with larger value first
Example: val[]= [10, 10, 10, 20], wt[] = [10, 10, 10, 30], capacity = 30
So, choosing higher values first also fails.
The idea is to always pick items greedily based on their value-to-weight ratio. Take the item with the highest ratio first, then the next highest, and so on, until the knapsack is full. If any item doesnβt fully fit, then take its fractional part according to the remaining capacity.
Steps to solve the problem:
240