Maximal subset sum smaller than a given value

guser

You are given an array of integers and a number k. The question is to find a subset such that the sum is maximal and smaller than given number k. I feel like there is a dynamic programming approach to solve this but I am not sure how to solve this problem efficiently.

David Eisenstat

The simple dynamic programs for this class of problems all use the same basic trick: for each successive prefix of the array, compute the set of its subset sums, depending on the fact that, when the input elements are bounded, this set has many fewer than 2^n elements (for the problem in question, less than 10,000,000 instead of 2^1000). This particular problem, in Python:

def maxsubsetsumlt(array, k):
    sums = {0}
    for elem in array:
        sums.update({sum_ + elem for sum_ in sums if sum_ + elem < k})
    return max(sums)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

If there is no subset sum equal to a given value, return subset sum closest to the value

From Java

find the subset of maximal sum in which the distance between consecutive elements is at most 6 from a given array

From Dev

find the subset of maximal sum in which the distance between consecutive elements is at most 6 from a given array

From Dev

Smallest sum of subarray with sum greater than a given value

From Dev

Calculating Minimal Subset With Given Sum

From Dev

Given a list of n integers , find the minimum subset sum greater than X

From Dev

Create an NSRange with a given minimal and maximal value

From Dev

Number of Subarray whose sum greater than given value

From Dev

Pandas: Efficient way to get first row with element that is smaller than a given value

From Dev

Numbers in a list smaller than a given number

From Dev

Highest power of two but smaller than given BigInteger

From Dev

addition of numbers, in a list, that are smaller than a given number

From Dev

How to subset a data frame by removing all rows from columns with a given string, and value less than X?

From Dev

Dynamic Programing to solve subset of given sum

From Dev

Most efficient way to find the smallest index where its value minus the value of a previous index is smaller than a given x?

From Dev

Most efficient way to find the smallest index where its value minus the value of a previous index is smaller than a given x?

From Dev

Ensure float to be smaller than exact value

From Dev

Max value from list that is smaller than X

From Dev

Are all the values on the Stack smaller than a passed value?

From Dev

Are all the values on the Stack smaller than a passed value?

From Dev

sizeof(struct) returns a SMALLER than expected value?

From Dev

Find number of elements smaller than a given element in BST

From Dev

Check if variable is a number smaller than a given number or equal to the text "QUIT"

From Dev

Sum until a given value is reached

From Dev

Finding maximal sum of happiness

From Dev

Given a sequence of n positive integers, find a subsequence of length k such that the sum over the absolute values of the differences is maximal

From Dev

Given a sequence of n positive integers, find a subsequence of length k such that the sum over the absolute values of the differences is maximal

From Dev

Recognizing if one value is larger or smaller than the second value (C++)

From Dev

checking if a value is greater than another and if not replacing with the smaller value idl to python

Related Related

  1. 1

    If there is no subset sum equal to a given value, return subset sum closest to the value

  2. 2

    find the subset of maximal sum in which the distance between consecutive elements is at most 6 from a given array

  3. 3

    find the subset of maximal sum in which the distance between consecutive elements is at most 6 from a given array

  4. 4

    Smallest sum of subarray with sum greater than a given value

  5. 5

    Calculating Minimal Subset With Given Sum

  6. 6

    Given a list of n integers , find the minimum subset sum greater than X

  7. 7

    Create an NSRange with a given minimal and maximal value

  8. 8

    Number of Subarray whose sum greater than given value

  9. 9

    Pandas: Efficient way to get first row with element that is smaller than a given value

  10. 10

    Numbers in a list smaller than a given number

  11. 11

    Highest power of two but smaller than given BigInteger

  12. 12

    addition of numbers, in a list, that are smaller than a given number

  13. 13

    How to subset a data frame by removing all rows from columns with a given string, and value less than X?

  14. 14

    Dynamic Programing to solve subset of given sum

  15. 15

    Most efficient way to find the smallest index where its value minus the value of a previous index is smaller than a given x?

  16. 16

    Most efficient way to find the smallest index where its value minus the value of a previous index is smaller than a given x?

  17. 17

    Ensure float to be smaller than exact value

  18. 18

    Max value from list that is smaller than X

  19. 19

    Are all the values on the Stack smaller than a passed value?

  20. 20

    Are all the values on the Stack smaller than a passed value?

  21. 21

    sizeof(struct) returns a SMALLER than expected value?

  22. 22

    Find number of elements smaller than a given element in BST

  23. 23

    Check if variable is a number smaller than a given number or equal to the text "QUIT"

  24. 24

    Sum until a given value is reached

  25. 25

    Finding maximal sum of happiness

  26. 26

    Given a sequence of n positive integers, find a subsequence of length k such that the sum over the absolute values of the differences is maximal

  27. 27

    Given a sequence of n positive integers, find a subsequence of length k such that the sum over the absolute values of the differences is maximal

  28. 28

    Recognizing if one value is larger or smaller than the second value (C++)

  29. 29

    checking if a value is greater than another and if not replacing with the smaller value idl to python

HotTag

Archive