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

Number of Subarray whose sum greater than given value

How to find the nth smallest subarray sum bigger than x in a progression where the first two numbers are given?

Finding subarray with given sum

smallest subsets with sum larger than value in python

Python: sum of values greater than last value

In an array find a subarray whose sum of elements multiplied by the smallest element in that subarray yields the greatest value

How to find the subarray with given sum

subset which gives the least sum greater than or equal to a given number

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

Find month have a sum greater than a given number

Maximal subset sum smaller than a given value

given a number p, print value of n at which sum of (1/1 + 1/2 + ... + 1/n) is greater than p

Smallest subset with sum greater or equal to k

Subset with smallest sum greater or equal to k

Sum list elements when sum is not greater than given interval, else skip element

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

Elasticsearch - Filter by sum of multiple fields greater than a specific value

Compare SUM of columns to see if equal or greater than threshold value

Liquid: If sum (plus) greater than

Find widest subarray with given sum (array slicing)

Maximum-sum subarray given constraints on indices

Bigtable python Client: How do I retrieve the smallest rowkey that is greater than a given value

largest sum of contiguous subarray No Larger than k

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

Remove all dataframe columns that have a sum lower than given value

Query where sum of two fields is less than given value

Finding rows with sum of a column which is lower than a given value in R

R: Count triplets with sum smaller than a given value

Finding the maximum elements whose sum is less than a given value in PyTorch