Minimum Cost to Buy Oranges Problem Statement

You are given a bag of capacity 'W' kg and a list 'cost' of costs for packets of oranges with different weights. Each element at the i-th position in the list indicates the price of a packet weighing (i+1) kg.

If an element at i-th position in 'cost' is -1, it means the (i+1) kg packet is unavailable.

Your task is to determine the minimum total cost to purchase exactly 'W' kg of oranges. If it is not possible to buy exactly W kg, return -1. Assume an infinite supply of any available packet type.

Example:

Input:
N = 5, W = 5
cost = [20, 10, 4, 50, -1]
Output:
14
Explanation:

Minimum cost to achieve 5 kg of oranges is using one 3 kg packet costing 4, and one 2 kg packet costing 10. Total = 4 + 10 = 14.

Constraints:

  • 1 ≤ N ≤ 1000
  • 1 ≤ W ≤ 1000
  • -1 ≤ cost[i] ≤ 1000000
  • Time Limit: 1 sec
AnswerBot
4mo

Find the minimum cost to purchase a specific weight of oranges given the cost of different weight packets.

  • Iterate through the list of costs and find the minimum cost to achieve the desired weight

  • Keep ...read more

Help your peers!
Select
Add answer anonymously...

Top Software Engineer Interview Questions Asked at Amazon

Q. Can you explain the difference between method overloading and method overriding ...read more
Q. Given a list of linked lists, each sorted individually, merge them into one sort...read more
Q. Implement the merge sort algorithm.
Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits