Complete the code to calculate the value-to-weight ratio for each item.
double ratio = items[i].value [1] items[i].weight;The value-to-weight ratio is calculated by dividing the value by the weight.
Complete the code to sort items by their value-to-weight ratio in descending order.
Arrays.sort(items, (a, b) -> Double.compare(b.[1], a.[1]));
We sort items by their ratio to pick the most valuable per weight first.
Fix the error in the loop condition to process items until capacity is full or all items are checked.
while (i < n && [1] > 0) {
The loop continues while there is capacity left to fill.
Fill both blanks to add the full item value or fractional value to totalValue and reduce capacity accordingly.
if (items[i].weight <= [1]) { totalValue += items[i].value; [2] -= items[i].weight; }
We check if the item fits in capacity, add full value, and reduce capacity by item's weight.
Fill all three blanks to add fractional value when item weight exceeds capacity and set capacity to zero.
else { totalValue += items[i].ratio [1] [2]; [3] = 0; }
When item weight is more than capacity, add fraction of value and set capacity to zero.
