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

  • Home
  • SEARCH
  • 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 8291775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:14:56+00:00 2026-06-08T13:14:56+00:00

Given a list of values (e.g. 10, 15, 20, 30, 70), values N (e.g.

  • 0

Given a list of values (e.g. 10, 15, 20, 30, 70), values N (e.g. 3) and S (e.g. 100), find a subset that satisfies :

  1. length of subset >= N
  2. sum of subset >= S

The sum of the subset should also be the least possible (the sum of remaining values should be the greatest possible) (e.g. result subset should be (10,20,70), not (15,20,70) which also satisfies 1. and 2.).

I was looking at some problems and solutions (Knapsack problem, Bin packing problem, …) but didn’t find them applicable. Similar problems on the internet were also not suitable for some reason (e.g. number of elements in subset was fixed).

Can someone point me in the right direction? Is there any other solution other than exhausting every possible combination?

Edit – working algorithm I implemented in ruby code, I guess it can be further optimized:

def find_subset_with_sum_and_length_threshold(vals, min_nr, min_sum)
    sum_map = {}
    vals.sort.each do |v|
      sum_map.keys.sort.each do |k|
        addends = sum_map[k] + [v]
        if (addends.length >= min_nr && k+v >= min_sum)
          return addends
        else
          sum_map[k+v] = addends
        end
      end
      sum_map[v] = [v] if sum_map[v].nil?
    end
  end
  • 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-08T13:14:59+00:00Added an answer on June 8, 2026 at 1:14 pm

    This is not very different from the 0-1 knapsack problem.

    Zero-initialize a matrix with S+U rows and N columns(U is the largest list value)
    Zero-initialize a bit array A with S+U elements
    For each value (v) in the list:
      For each j<S:
        If M[N-1,j] != 0 and M[N-1, j + v] == 0:
          M[N-1, j + v] = v
          A[j + v] = true
      For i=N-2 .. 0:
        For each j<S:
          If M[i,j] != 0 and M[i+1, j + v] == 0:
            M[i+1, j + v] = v
      M[0,v] = v
    Find first nonzero element in M[N-1,S..S+U]
    Reconstruct other elements of the subset by subtracting found value from its\
      index and using the result as index in preceding column of the matrix\
      (or in the last column, depending on the corresponding bit in 'A').
    

    Time complexity is O(L*N*S), where L is the length of the list, N and S are given limits.

    Space complexity is O(L*N).


    Zero-initialize an integer array A with S+U elements
    i=0
    For each value (v) in the list:
      For each j<S:
        If A[j] != 0 and A[j + v] < A[j] + 1:
          A[j + v] = A[j] + 1
          V[i,j + v] = v
          P[i,j + v] = I[j]
          I[j + v] = i
      If A[v] == 0:
        A[v] = 1
        I[v] = i
      ++i
    Find first element in A[S..S+U] with value not less than N
    Reconstruct elements of the subset using matrices V and P.
    

    Time complexity is O(L*S), where L is the length of the list, S is given limit.

    Space complexity is O(L*S).


    Algorithm that also minimizes the subset size:

    Zero-initialize a boolean matrix with S+U rows and N columns\
      (U is the largest list value)
    Zero-initialize an integer array A with S+U elements
    i=0
    For each value (v) in the list:
      For each j<S:
        If A[j] != 0 and (A[j + v] == 0) || (A[j + v] > A[j] + 1)):
          A[j + v] = A[j] + 1
          V[i,N-1,j + v] = v
          P[i,N-1,j + v] = (I[j,N-1],N-1)
          I[j+v,N-1] = i
      For k=N-2 .. 0:
        For each j<S:
          If M[k,j] and not M[k+1, j + v]:
            M[k+1, j + v] = true
            V[i,k+1,j + v] = v
            P[i,k+1,j + v] = (I[j,k],k)
            I[j+v,k+1] = i
      For each j<S:
        If M[N-1, j]:
          A[j] = N-1
      M[0,v] = true
      I[v,0] = i
      ++i
    Find first nonzero element in A[N-1,S..S+U] (or the first element with smallest\
      value or any other element that suits both minimization criteria)
    Reconstruct elements of the subset using matrices V and P.
    

    Time complexity is O(L*N*S), where L is the length of the list, N and S are given limits.

    Space complexity is O(L*N*S).

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

Sidebar

Related Questions

Given a list of values: data = [100, 80, 200, 20, 300, 100, 400,
Given the code below, how do I compare a List of objects's values with
Given an R list, I wish to find the index of a given list
Given a list a containing vectors of unequal length and a vector b containing
Given a list of numbers, how does one find differences between every ( i
I'm trying to find an item in a list of values based on another
In Eclipse is there any way that given a class with a list of
The original code was somehow complex, i simplify it as: Given: list of class
Given a List such as List(1, 2, 3, 4, 5, 6, 7) what is
Given a list like this: num = [1, 2, 3, 4, 5] There are

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.