Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6380 |

@mb1973 | 5704 |

@Quandray | 5245 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5046 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3523 |

@mantu_singh | 3510 |

@sushant_a | 3459 |

Complete Leaderboard | |

Handle | Score |

@cfwong8 | 1134 |

@gurshehzadsingh | 676 |

@nithinreddy3210 | 610 |

@Run_for_life | 570 |

@divyagupta221100 | 557 |

@jha8768 | 553 |

@AkashLahoty | 537 |

@aayushdesai01 | 526 |

@riyakumaribkj | 524 |

@amansinha74458 | 503 |

@dreamerdreamer | 488 |

Complete Leaderboard |

Given an array **cost[]** of positive integers of size **N** and an integer **W**, cost[i] represents the cost of **‘i’** kg packet of oranges, the task is to find the minimum cost to buy **W** kgs of oranges. If it is not possible to buy exactly **W** kg oranges then the output will be -1

**Note:**

1. cost[i] = -1 means that ‘i’ kg packet of orange is unavailable

2. It may be assumed that there is infinite supply of all available packet types.

**Example 1:**

**Input**: N = 5, arr[] = {20, 10, 4, 50, 100}
W = 5
**Output:** 14
**Explanation**: choose two oranges to minimize
cost. First orange of 2Kg and cost 10.
Second orange of 3Kg and cost 4.

**Example 2:**

**Input**: N = 5, arr[] = {-1, -1, 4, 3, -1}
W = 5
**Output:** -1
**Explanation**: It is not possible to buy 5
kgs of oranges

**Your Task: **

You don't need to read input or print anything. Complete the function ** minimumCost() **which takes

1 ≤ N, W ≤ 10

-1 ≤ cost[i] ≤ 10

cost[i] ≠ 0

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Minimum cost to fill given weight in a bag

...