Smallest sum of subarray with sum greater than a given value

Ilya Gazman

Input: Array of N positive numbers and a value X such that N is small compared to X
Output: Subarray with sum of all its numbers equal to Y > X, such that there is no other subarray with sum of its numbers bigger than X but smaller than Y.

Is there a polynomial solution to this question? If so, can you present it?

Orhan Tuncer

As the other answers indicate this is a NP-Complete problem which is called the "Knapsack Problem". So there is no polynomial solution. But it has a pseudo polynomial time algorithm. This explains what pseudo polynomial is.

A visual explanation of the algorithm.

And some code.

If this is work related (I met this problem a few times already, in various disguises) I suggest introducing additional restrictions to simplify it. If it was a general question you may want to check other NP-Complete problems as well. One such list.

Edit 1:

AliVar made a good point. The given problem searches for Y > X and the knapsack problem searches for Y < X. So the answer for this problem needs a few more steps. When we are trying to find the minimum sum where Y > X we are also looking for the maximum sum where S < (Total - X). The second part is the original knapsack problem. So;

  1. Find the total
  2. Solve knapsack for S < (Total - X)
  3. Subtrack the list of items in knapsack solution from the original input.
  4. This should give you the minimum Y > X

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to create a cumulative sum column in python if column value is greater than other value

From Dev

Maximum Sum SubArray

From Dev

Find the largest sum subarray from the given array using segment trees

From Dev

Find numbers of subarray of an array whose sum is divided by given number

From Dev

smallest number greater than given number which is a palindrome

From Dev

Select array elements that are greater than 5% of a sum

From Dev

Maximum subarray sum such that sum is odd

From Dev

Number of Subarray whose sum greater than given value

From Dev

MySQL select records with sum greater than threshold

From Dev

How to select where sum of fields is greater than a value in MongoDB

From Dev

sum if greater than in r

From Dev

Find widest subarray with given sum (array slicing)

From Dev

Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

From Dev

Sum until a given value is reached

From Dev

largest sum of contiguous subarray No Larger than k

From Dev

Using a map to find subarray with given sum (with negative numbers)

From Dev

Maximum-sum subarray given constraints on indices

From Dev

Python - Get sum of pair in a list which is greater than a value with no index being used in multiple pairs

From Dev

find the maximum value of sum of subarray modulo M

From Dev

Finding subarray with given sum

From Dev

Excel: count the number of cells in a column until their sum is greater than a set value

From Dev

Given two arrays with same size, find the couple of elements from both arrays that make sum with the smallest absolute value

From Dev

Find the minimum set of integers whose sum is greater than a given integer

From Dev

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

From Dev

Maximum sum in a contiguous subarray of a given 2D array

From Dev

Maximal subset sum smaller than a given value

From Dev

d3.js v4 Partition where parent's value is greater than sum of its children (node.sum)

From Dev

the sum of cells that meet a greater than and less than criteria

From Dev

Can we use IntStream#sum, If sum of elements is greater than the Integer.MAX_VALUE?

Related Related

  1. 1

    How to create a cumulative sum column in python if column value is greater than other value

  2. 2

    Maximum Sum SubArray

  3. 3

    Find the largest sum subarray from the given array using segment trees

  4. 4

    Find numbers of subarray of an array whose sum is divided by given number

  5. 5

    smallest number greater than given number which is a palindrome

  6. 6

    Select array elements that are greater than 5% of a sum

  7. 7

    Maximum subarray sum such that sum is odd

  8. 8

    Number of Subarray whose sum greater than given value

  9. 9

    MySQL select records with sum greater than threshold

  10. 10

    How to select where sum of fields is greater than a value in MongoDB

  11. 11

    sum if greater than in r

  12. 12

    Find widest subarray with given sum (array slicing)

  13. 13

    Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

  14. 14

    Sum until a given value is reached

  15. 15

    largest sum of contiguous subarray No Larger than k

  16. 16

    Using a map to find subarray with given sum (with negative numbers)

  17. 17

    Maximum-sum subarray given constraints on indices

  18. 18

    Python - Get sum of pair in a list which is greater than a value with no index being used in multiple pairs

  19. 19

    find the maximum value of sum of subarray modulo M

  20. 20

    Finding subarray with given sum

  21. 21

    Excel: count the number of cells in a column until their sum is greater than a set value

  22. 22

    Given two arrays with same size, find the couple of elements from both arrays that make sum with the smallest absolute value

  23. 23

    Find the minimum set of integers whose sum is greater than a given integer

  24. 24

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

  25. 25

    Maximum sum in a contiguous subarray of a given 2D array

  26. 26

    Maximal subset sum smaller than a given value

  27. 27

    d3.js v4 Partition where parent's value is greater than sum of its children (node.sum)

  28. 28

    the sum of cells that meet a greater than and less than criteria

  29. 29

    Can we use IntStream#sum, If sum of elements is greater than the Integer.MAX_VALUE?

HotTag

Archive