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 8580999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:56:45+00:00 2026-06-11T20:56:45+00:00

So if I was given a sorted list/array i.e. [1,6,8,15,40], the size of the

  • 0

So if I was given a sorted list/array i.e. [1,6,8,15,40], the size of the array, and the requested number..

How would you find the minimum number of values required from that list to sum to the requested number?

For example given the array [1,6,8,15,40], I requested the number 23, it would take 2 values from the list (8 and 15) to equal 23. The function would then return 2 (# of values). Furthermore, there are an unlimited number of 1s in the array (so you the function will always return a value)

Any help is appreciated

  • 1 1 Answer
  • 0 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. Editorial Team
    Editorial Team
    2026-06-11T20:56:47+00:00Added an answer on June 11, 2026 at 8:56 pm

    The NP-complete subset-sum problem trivially reduces to your problem: given a set S of integers and a target value s, we construct set S’ having values (n+1) xk for each xk in S and set the target equal to (n+1) s. If there’s a subset of the original set S summing to s, then there will be a subset of size at most n in the new set summing to (n+1) s, and such a set cannot involve extra 1s. If there is no such subset, then the subset produced as an answer must contain at least n+1 elements since it needs enough 1s to get to a multiple of n+1.

    So, the problem will not admit any polynomial-time solution without a revolution in computing. With that disclaimer out of the way, you can consider some pseudopolynomial-time solutions to the problem which work well in practice if the maximum size of the set is small.

    Here’s a Python algorithm that will do this:

    import functools
    S = [1, 6, 8, 15, 40] # must contain only positive integers
    @functools.lru_cache(maxsize=None) # memoizing decorator
    def min_subset(k, s):
        # returns the minimum size of a subset of S[:k] summing to s, including any extra 1s needed to get there
        best = s # use all ones
        for i, j in enumerate(S[:k]):
            if j <= s:
                sz = min_subset(i, s-j)+1
                if sz < best: best = sz
        return best
    
    print min_subset(len(S), 23) # prints 2
    

    This is tractable even for fairly large lists (I tested a random list of n=50 elements), provided their values are bounded. With S = [random.randint(1, 500) for _ in xrange(50)], min_subset(len(S), 8489) takes less than 10 seconds to run.

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

Sidebar

Related Questions

Given two sorted arrays: A and B . The size of array A is
I'm using Tcl. I have a sorted list of real numbers. Given the number
Given a sorted list of something (a,a,b,c,c) What would be the most efficient way
the question is like this: there is a sorted list of n numbers. Given
How would I print the multiples of a list of given numbers in a
Given a BST with unique integers and a number K. Find a pair (
Given a sorted list of strings and the last entry is null. You cannot
I have a one-dimensional grid that is defined as a list of sorted floating
I am given a set of size L and want to generate every sorted
I'm doing some research on ranking algorithms, and would like to, given a sorted

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.