Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 91141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:59:00+00:00 2026-05-10T22:59:00+00:00

Given an array of items, each of which has a value and cost ,

  • 0

Given an array of items, each of which has a value and cost, what’s the best algorithm determine the items required to reach a minimum value at the minimum cost? eg:

Item: Value -> Cost ------------------- A     20   -> 11 B     7    -> 5 C     1    -> 2  MinValue = 30 naive solution: A + B + C + C + C. Value: 30, Cost 22 best option: A + B + B.            Value: 34, Cost 21 

Note that the overall value:cost ratio at the end is irrelevant (A + A would give you the best value for money, but A + B + B is a cheaper option which hits the minimum value).

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:59:01+00:00Added an answer on May 10, 2026 at 10:59 pm

    This is the knapsack problem. (That is, the decision version of this problem is the same as the decision version of the knapsack problem, although the optimization version of the knapsack problem is usually stated differently.) It is NP-hard (which means no algorithm is known that is polynomial in the ‘size’ — number of bits — in the input). But if your numbers are small (the largest ‘value’ in the input, say; the costs don’t matter), then there is a simple dynamic programming solution.

    Let best[v] be the minimum cost to get a value of (exactly) v. Then you can calculate the values best[] for all v, by (initializing all best[v] to infinity and):

    best[0] = 0 best[v] = min_(items i){cost[i] + best[v-value[i]]} 

    Then look at best[v] for values upto the minimum you want plus the largest value; the smallest of those will give you the cost.

    If you want the actual items (and not just the minimum cost), you can either maintain some extra data, or just look through the array of best[]s and infer from it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have variable call items having array of items which contains following value [[#<Item
Given the following array of json objects: var items = [ {id:1, parentId:0, name:'item1'},
I have an array of objects, each of which is assigned an ID when
I have a collection of entities, which represents a tree. Each entity has a
This problem is taken from interviewstreet.com Given array of integers Y=y1,...,yn, we have n
given an array: array = [16 16 16 22 23 23 23 25 52
Given an Array A of n subArrays Sn, how can I select the Array
Given an array of integers A[1...n-1] where N is the length of array A.
Given an array of ids $galleries = array(1,2,5) I want to have a SQL
Given an array with n values, for example: $arr[] = 'ABCDEFABC'; $arr[] = 'ABCDEFDEF';

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.