Close Menu
    Facebook LinkedIn YouTube WhatsApp X (Twitter) Pinterest
    Trending
    • AI evolves itself to speed up scientific discovery
    • Australia’s privacy commissioner tried, in vain, to sound the alarm on data protection during the u16s social media ban trials
    • Nothing Phone (4a) Pro Review: A Close Second
    • Match Group CEO Spencer Rascoff says growing women’s share on Tinder is his “primary focus” to stem user declines; Sensor Tower says 75% of Tinder users are men (Kieran Smith/Financial Times)
    • Today’s NYT Connections Hints, Answers for April 20 #1044
    • AI Machine-Vision Earns Man Overboard Certification
    • Battery recycling startup Renewable Metals charges up on $12 million Series A
    • The Influencers Normalizing Not Having Sex
    Facebook LinkedIn WhatsApp
    Times FeaturedTimes Featured
    Monday, April 20
    • Home
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    • More
      • AI
      • Robotics
      • Industries
      • Global
    Times FeaturedTimes Featured
    Home»Artificial Intelligence»The Subset Sum Problem Solved in Linear Time for Dense Enough Inputs
    Artificial Intelligence

    The Subset Sum Problem Solved in Linear Time for Dense Enough Inputs

    Editor Times FeaturedBy Editor Times FeaturedDecember 18, 2025No Comments32 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Telegram Email WhatsApp Copy Link


    , I’ll current an answer to the Subset Sum Drawback, which has linear time complexity O(n), if all of the ‘n’ enter values are “shut sufficient” to one another. We are going to see shortly what that situation really means, in addition to why it would typically be stored (or nearly stored) in observe. For the inputs which are “nearly shut” to one another, the algorithm nonetheless has a sure likelihood of working quick sufficient.

    This text is organized within the following manner:

    • In chapter 1, we are going to recall the Subset sum downside and present in all probability probably the most trivial answer to it, based mostly on the brute-force method.
    • Chapter 2 recollects the category of NP-complete issues and the way the Subset sum downside is expounded to it.
    • Chapter 3 evaluations the generally used answer based mostly on Dynamic programming method, and highlights why it’s a pseudo-polynomial algorithm.
    • In chapter 4, I’ll introduce the brand new algorithm, referred to as “Interval-based answer”.
    • Chapter 5 will present that, equally to the Dynamic programming method, the Interval-based algorithm may restore the precise subset of the objects that kind given goal sum, and never simply reply if the goal sum is achievable or not.
    • Lastly, in chapter 6, we are going to see why the Interval-based algorithm really reaches linear time complexity, if all of the ‘n’ enter values are shut sufficient to one another (in addition to we are going to perceive there what the “shut sufficient” situation really means).

    1. Recalling the Subset sum downside

    The definition of the Subset Sum Drawback (SSP) is sort of easy:

    We’re given ‘n’ enter integers X={x1, x2, …, xn}, and a goal sum ‘q’. It’s needed to determine if there exists such a subset ‘Y’ of these ‘n’ integers, numbers of which is able to sum up precisely to ‘q’. If it does, the issue may also ask to report all of the numbers of the subset ‘Y’.

    Right here is an instance of enter to the SSP:

    The enter set ‘X’ with ‘n=8’ enter values is offered on the proper.
    The goal sum ‘q=22’ is proven on the left.

    And right here is its answer:

    Selecting the enter values 14, 2, and 6 leads to the goal sum ‘q’: “14+2+6=22”.

    Word, there will be circumstances with multiple answer, for a given goal sum ‘q’:

    For the goal sum ‘q=30’, we have now two choices: “2+13+15=30” and “6+24=30”.

    Additionally, there will be circumstances when there is no such thing as a answer in any respect, that means that there is no such thing as a such subset, integers of which is able to accumulate precisely to the given goal sum ‘q’:

    No subset of the enter objects ‘X’ will sum as much as 25.

    On this article, we are going to take into account solely optimistic enter values xi ∈ X. Nonetheless, all of the concepts to be offered beneath will be utilized (with minor modifications) additionally to the case when there are adverse enter values as properly.

    Within the Subset sum downside (SSP), a slight change within the enter values can lead to a whole change of the reply. Like, if there are numerous subsets that kind the goal sum ‘q’, it doesn’t imply that there will likely be even one subset, that varieties the goal sum ‘q+1’. This truth additionally makes the SSP not a simple one to unravel.

    Even when there aren’t many enter numbers, it would nonetheless be troublesome to unravel the issue on a chunk of paper. Typically, we might want to take into account all totally different subsets and examine which one in every of them accumulates in direction of ‘q’. That strategy lies within the trivial answer of SSP, brute-force, which simply enumerates all doable subsets.

    The thought for brute-force implementation is: contemplating that there exists such a subset Y⊆X, objects of which accumulate in direction of ‘q’, let’s deal with the final enter merchandise xn. There are two situations:

    • both xn participates within the outcome subset ‘Y’,
    • or it doesn’t.

    Having that stated, if xn does take part in ‘Y’ (xn∈Y), then we must always proceed trying to find such a subset from the diminished set of numbers “X {xn} = {x1, x2, …, xn-1}”, which is able to accumulate now to “q–xn”:

    Whereas summing up ‘q=22’, we determined to take enter merchandise ‘6’ into the outcome subset ‘Y’.
    So, from the remaining numbers, we must always construct up the sum “22-6=16”.

    In any other case, if the case is that xn doesn’t take part in ‘Y’ (xn∉Y), then we must always proceed trying to find such a subset from the diminished set of numbers “X {xn} = {x1, x2, …, xn-1}”, which is able to itself accumulate to the identical goal sum ‘q’:

    Whereas summing up ‘q=22’, we determined to not take enter merchandise ‘6’ into the outcome subset ‘Y’.
    So, from the remaining numbers, we must always construct up the identical sum “22”.

    Absolutely, we don’t know prematurely which case is true; that’s why the brute-force algorithm simply tries one case after one other.

    When attempting to unravel the diminished downside (i.e., discovering a correct subset from the diminished set of numbers “X {xn} = {x1, x2, …, xn-1}”), the brute-force algorithm applies the identical logic recursively. So, no matter which department we took, subsequent, the worth xn-1 will likely be thought-about, and at first, we are going to search for an answer the place xn-1 does take part within the outcome subset, after which we’ll search for such an answer the place it doesn’t.

    The choice diagram for the Subset Sum Drawback. We begin on the rightmost state, and on each step we both take xi into the outcome subset (the higher department), or we don’t take it (the decrease department). The numbers close to each state present the remaining sum that’s needed to assemble.

    Whereas recursion deepens, if the remaining goal sum turns into zero, it signifies that we’re at present on the right department, and the thought-about numbers already accumulate to the unique goal sum ‘q’.

    Following the right path (purple arrows), we are going to ultimately construct the goal sum ‘q=22’.

    The pseudo-code of the talked about brute-force algorithm turns into like this:

    // Searches for such a subset of ‘X’ which has a sum equal 
    // to ‘q’, and locations it into ‘Y’. The set ‘X’ is assumed 
    // to have ‘n’ integers.
    process ssp_brute_force( 
    		X[1..n]: set of Integers, 
    		n: Integer, 
    		q: Integer, 
    		Y: Reference to set of Integers )
    	if q==0 then
    			// By choosing the right integers into ‘Y’, we have now
    			// iteratively diminished ‘q’ to zero, so the answer 
    			// subset is already in ‘Y’.
    		print Y
    		return  // No must do anything on this department
    	if n==0 then
    			// ‘q’ isn't zero, whereas the enter set ‘X’ is 
    			// exhausted. This department didn’t led to an answer.
    		return
    	// Strive with ‘X[n]’ within the answer subset
    	place X[n] into Y
    	ssp_brute_force( X{X[n]}, n-1, q-X[n], Y )
    			// Proceed looking with the diminished set 
    			// (‘X’ with out the final integer X[n]), for 
    			// such a subset, which sums to ‘q-X[n]’.
    	take away X[n] from Y
    	// Strive with out ‘X[n]’
    	ssp_brute_force( X{X[n]}, n-1, q, Y )
    			// Proceed looking with the diminished set 
    			// (‘X’ with out the final integer X[n]), for 
    			// such a subset, which nonetheless sums to ‘q’.

    Upon the pseudo-code, we see that fixing the issue for ‘n’ enter objects requires fixing two issues, every with ‘n-1’ enter objects. Every of those will, in its flip, require fixing two diminished issues, this time with ‘n-2’ enter objects, ensuing general in 4 issues with ‘n-2’ enter objects every. This fashion, the variety of issues doubles on every arrival of a brand new enter merchandise, which makes the time complexity of the brute-force algorithm exponential – O(2n).

    The peak of the choice diagram is exponential – 2n, whereas its width equals ‘n’.

    In observe, this algorithm can be utilized provided that ‘n’ is small enough.

    Nonetheless, the following algorithms for the Subset sum downside which will likely be described on this article, nonetheless depend on the logic of “utilizing vs. not utilizing” the present merchandise.


    2. Subset sum and NP-complete issues

    There’s a class of issues in Laptop Science, referred to as “NP-complete”, which consists of such issues which are thought-about troublesome to unravel. The described Subset Sum Drawback belongs to the NP-complete class. Extra exactly, to ensure that an issue to be NP-complete, the next standards have to be met:

    1. It’s a decision problem, that means that for any enter to the issue, the output is both “sure” or “no”.
      • Relating to the Subset sum downside, any enter consisting of a set of enter values X = {x1, x2, …, xn} and a goal sum ‘q’ leads to a “sure” or “no” reply: we both can choose a subset which accumulates in direction of ‘q’, or we are able to’t.
    2. When the reply is “sure”, this may be demonstrated by means of the existence of a brief (polynomial size) answer.
      • In regard to the SSP, whether it is doable to select and current such a subset Y⊆X, we are able to simply sum up all its integers, and examine if that actually equals ‘q’ or not.
    3. The correctness of every answer will be verified shortly (specifically, in polynomial time), and a brute-force search algorithm can discover a answer by attempting all doable options.
      • The brute-force answer for Subset sum downside is what we have now simply recalled within the earlier chapter.
    4. The issue can be utilized to simulate each different downside for which we are able to shortly confirm {that a} answer is appropriate. Therefore, if we may discover options of some NP-complete downside shortly, we may shortly discover the options of each different downside to which a given answer will be simply transformed.

    The final assertion exhibits that there are additionally different NP-complete issues, in addition to that any of them will be transformed into one other. So, if a polynomial-time algorithm for one NP-complete downside will likely be discovered, we may use it to unravel another NP-complete downside, by changing it to the previous one. Here’s a frequent conversion diagram between totally different NP-complete issues:

    We see that, for instance, the “Boolean method satisfiability downside” will be transformed into the “Subset sum downside”, whereas the latter one will be transformed into the “Knapsack downside”.


    3. The Dynamic programming answer of the Subset sum downside

    The frequent answer of the Subset Sum Drawback (SSP) makes use of a Dynamic Programming method. The precept of any Dynamic Programming algorithm lies in initially fixing issues of smaller sizes, and later utilizing these options to unravel the preliminary giant downside.

    Let’s “S” be a boolean matrix having dimensions “S[0..n][0..q]”, with
    “S[i][p]” denoting if we are able to collect the sum ‘p’ by selecting solely between the primary ‘i’ enter objects.

    X = {8, 4, 11, 3, 9},
    q = 28.

    The matrix “S” with dimensions “(n+1)*(q+1)”.

    Within the first chapter, we already expressed the worth “S[n][q]” in a recursive manner. “S[n][q]” denotes whether or not we are able to get hold of the sum ‘q’ by selecting between all of the ‘n’ enter integers, which is the reply to the preliminary downside. We addressed two circumstances there:

    • If the final merchandise ‘xn’ participates within the outcome subset ‘Y’, then we must always be capable of get hold of the sum ‘q–xn’, by selecting between the primary ‘n-1’ enter objects. In different phrases, “S[n-1][q–xn]” ought to be equal to “true”.
    • In any other case, if the final merchandise ‘xn’ doesn’t take part within the outcome subset ‘Y’, then we must always handle to acquire the goal sum ‘q’ by selecting additionally between the primary ‘n-1’ enter objects. In different phrases, “S[n-1][q]” ought to be equal to “true” then.

    There isn’t a third choice. If both of those two circumstances is glad, then the goal sum ‘q’ is reachable. In the event that they each are glad, it signifies that ‘q’ will be obtained each by choosing ‘xn’ and by not choosing it into the outcome subset ‘Y’.

    So the method for the preliminary downside turns into:

    [begin{equation*}
    S[n][q] = S[n-1][q-x_n] or S[n-1][q]
    finish{equation*}]

    And it really works not just for “S[n][q]” but additionally for any “S[i][p]”, because the logic stays the identical:

    [begin{equation*}
    S[i][p] = S[i-1][p-x_i] or S[i-1][p]
    finish{equation*}]

    Now, when filling the matrix “S[0..n][0..q]”, we see that the worth of any cell ‘S[i][p]’ relies upon solely on two different cells, each located within the row above:

    Worth of the cell S[3][14] (the sunshine purple one) relies upon solely on the values of the 2 cells
    S[2][14] and S[2][3] (the sunshine blue ones).

    This implies we are able to calculate the matrix in top-down order, which is able to assure that in the intervening time “S[i][p]” is being calculated, we already know the values of each “S[i-1][p–xi]” and “S[i-1][p]”.

    The matrix will likely be crammed within the top-down course, calculating cells of each row from left to proper.
    The yellow cells aren’t calculated but.

    What’s required to begin the calculation is the content material of the primary row “S[0][p], p∈[0..q]”. The cell “S[0][p]” denotes whether it is doable to assemble sum ‘p’, having solely the primary 0 enter objects (i.e. having no merchandise in any respect)”. Clearly, the reply is “false” if “p>0” and is “true” provided that “p==0” (we are able to collect the sum 0, with out utilizing any merchandise).

    Whatever the set of enter objects ‘X’, the primary row of desk “S” at all times has “S[0][0] = true”, and “S[0][p] = false”, when “p≥1”. The yellow cells aren’t calculated but.

    Having that stated, we are able to calculate all cells of the desk within the top-down order. For our enter set, the outcome will likely be:

    The final cell “S[5][28] = true”, which signifies that the goal sum “q=28” will be obtained
    by selecting between all 5 enter objects.

    The pseudo-code of the Dynamic Programming answer turns into:

    // Given an n-long set of integers ‘X’, returns whether it is doable 
    // to search out such a subset of it, which sums as much as ‘q’.
    operate ssp_dynamic_programming( 
    		X[1..n]: set of Integers, 
    		n: Integer, 
    		q: Integer ) : Boolean
    	S[0..n][0..q]: Matrix of Booleans
    	// Fill first row of ‘S’
    	S[0][0] := true
    	for p in [1..q]
    		S[0][p] := false
    	// Fill content material of the matrix
    	for i in [1..n]
    		for p in [0..q]
    			if p < x[i]
    				S[i][p] := S[i-1][p]
    			else
    				S[i][p] := S[i-1][p-x[i]] or S[i-1][p]
    	// The reply is within the bottom-right cell
    	return S[n][q]

    The underside-right cell “S[n][q]” will comprise the reply to the unique downside, stating if we are able to collect the sum ‘q’ by selecting between all of the ‘n’ enter objects.

    The offered answer requires filling the matrix “S”, which has “(n+1)*(q+1)” cells. Calculating every cell is finished in O(1) time. Therefore, the time complexity of the Dynamic Programming algorithm turns into O(nq). This answer is known as pseudo-polynomial, as a result of right here the issue ‘q’ isn’t proportional to any polynomial of the issue’s measurement. The truth is, ‘q’ will be proportional even to the exponent of downside’s measurement.


    4. Introducing the Interval-based algorithm

    Within the common case, the desk “S” crammed within the earlier chapter can have fairly unpredictable content material on the finish. Nonetheless, if the enter values “X = {x1, x2, …, xn}” do fulfill sure circumstances, rows of the crammed desk would possibly comprise many adjoining cells with “true” values, in addition to many adjoining cells with “false” values.

    X = {9, 4, 3, 12, 5},
    q = 30.

    For the offered enter, the final row accommodates one lengthy vary of true-values, ranging from S[5][12] until S[5][21].

    Within the present row, let’s denote these adjoining cells with a worth of “true” as “true-intervals”, and the adjoining cells with a worth of “false” as “false-intervals”.

    Some true-intervals are highlighted in inexperienced, whereas some false-intervals in purple.

    If we all know prematurely that the calculated desk “S” can have sufficiently lengthy true-intervals and/or sufficiently lengthy false-intervals on the finish, it can make sense to compute “S” not cell-based (as we did within the earlier chapter), however interval-based.

    Then, as a substitute of representing each row as a boolean array, we are going to characterize it as a sequence of ordered true-intervals. Throughout the present row, the true-intervals by no means intersect, so we are able to preserve them sorted by their beginning factors. Additionally, for simplicity of the formulation to return later, when denoting a true-interval, we are going to imply a half-opened vary [a..b). So, for example, a true-interval [5..8) will mean that on the current row, the cells 5, 6, and 7 are equal to “true”, while cell 8 is “false”.

    The last row contains the following true-intervals: { [0,1), [3,6), [7,10), [12,22), [24,27), [28,31) }. All those are highlighted in green. The set of true-intervals uniquely identifies the content of the last row of the cell-based table.

    Now, having the true-intervals (later denoted just as “intervals”, as false-intervals do not play a role in the algorithm to be described) of the previous row ‘i-1’, how should we compute the intervals of the current row ‘i’? Let’s recall the formula for filling the table:

    [begin{equation*}
    S[i][p] = S[i-1][p-x_i] or S[i-1][p]
    finish{equation*}]

    If altering it for a second, and assuming that there’s solely the second a part of the OR-expression:

    [begin{equation*}
    S[i][p] = S[i-1][p]
    finish{equation*}]

    it can end in all cells of the earlier row being copied into the present row. In different phrases, for that it might be simply sufficient to repeat the intervals from row ‘i-1’ to row ‘i’.

    Copying intervals from row ‘i-1’ (gentle inexperienced bricks) to row ‘i’ (darkish inexperienced bricks).

    Then again, if altering the method within the different manner, assuming that there’s solely the primary a part of the OR-expression:

    [begin{equation*}
    S[i][p] = S[i-1][p-x_i]
    finish{equation*}]

    it can nonetheless end in copying all of the cells from the earlier row to the present row, however this time shifted by ‘xi’ positions rightwards. So that’s what will likely be essential to do additionally with the intervals:

    Copy-and-shifting rightwards by ‘xi’ all of the intervals from row ‘i-1’ (gentle inexperienced bricks) to row ‘i’ (darkish inexperienced bricks).

    Lastly, referring to the unique method:

    [begin{equation*}
    S[i][p] = S[i-1][p-x_i] or S[i-1][p]
    finish{equation*}]

    it requires us to do “OR” on the 2 obtained sequences of intervals, which is identical as uniting them geometrically.

    Geometrically uniting the unique and shifted rightwards by ‘xi’ sequences of true-intervals (first two rows) leads to the sequence of true-intervals for the following merchandise ‘xi’ (the underside row).

    Summarizing what was stated above, when filling the desk of true-intervals, to compute the content material of row ‘i’ we must always:

    • shift the content material of row ‘i-1’ to the proper by ‘xi’ positions, and
    • unite it with the unique content material of row ‘i-1’.

    Word, this additionally signifies that the span of row ‘i’ (proper endpoint of the right-most interval) will at all times be by ‘xi’ models better than the span of row ‘i-1’.

    Spans of rows develop by sizes, equal to values ‘xi’. Span of row “x1=9” is “9”. Span of row “x2=4” is “9+4=13”. Span of row “x3=3” is “9+4+3=16”, and so forth.

    So the span of any row ‘i’ will be calculated as:

    [begin{equation*}
    span(i) = 1 + x_1 + x_2 + … + x_i = 1 + sum_{k=1}^i x_k
    end{equation*}]

    the place the free time period “1” comes due to all of the intervals being half-open (the preliminary interval at row 0 is [0, 1), so “span(0) = 1”).

    Considering that the previous row ‘i-1’ has ‘c’ intervals, shifting them all by xi will obviously require O(c) time. Calculating the union of 2 sequences, each having ‘c’ intervals, can also be performed in O(c) time if scanning both sequences from left to right
    simultaneously. This means that the transition from the previous row to the current one requires O(c) time.

    Assuming that the maximal number of intervals on any row is ‘c’, the time complexity for the Interval-based algorithm becomes O(nc).

    The important note that we should make here is that the value ‘c’ significantly depends on the order of input values “X = {x1, x2, …, xn}”. Here is an example with “n=5” values, which at the end produces lots of intervals in the table.

    X = {6, 11, 4, 2, 5}.

    The ‘n=5’ input values are considered in arbitrary order. This results in lots of short intervals in the middle of the table.

    And here is the problem solved with the same set of input values “X={x1, x2, …, xn}”, but arranged in a different order, more precisely, in ascending order.

    X = {2, 4, 5, 6, 11}.

    The same input set with ‘n=5’ values, but now arriving in increasing order. We see that numbers of intervals in intermediate rows are reduced significantly.

    Such a behavior is quite intuitive: if we want the intervals of the current row to remain as few as possible, their span should grow as slowly as possible. And an obvious way to achieve that is to consider all the input values xi in ascending order.


    5. Restoring the exact subset of values

    The Dynamic programming solution of the Subset sum problem, which we reviewed in chapter 3, not only answers if it is possible to obtain given sum ‘q’ from the input items “X = {x1, x2, …, xn}”, but also specifies the exact subset of ‘X’ (let’s denote it by ‘Y’, ‘Y⊆X’), items of which sum up to ‘q’. To retrieve ‘Y’, we must move over the table in the opposite direction – from bottom to top.

    The last cell of the table “S[n][q]”, which accommodates the reply to the unique downside, was calculated by the method:

    [begin{equation*}
    S[n][q] = S[n-1][q-x_n] or S[n-1][q]
    finish{equation*}]

    If “S[n][q]” seems to be true (that means that the goal sum ‘q’ is reachable), it signifies that at the very least one of many 2 operands of the OR-expression can also be “true”. So, we are able to examine 2 circumstances:

    • if “S[n-1][q] == true”, it signifies that the goal sum ‘q’ will be obtained additionally by selecting solely between the primary ‘n-1’ enter objects. So the final merchandise ‘xn’ doesn’t take part in ‘Y’, and we are able to proceed constructing ‘Y’ solely from the primary ‘n-1’ objects.
    • if “S[n-1][q–xn] == true”, it signifies that the primary ‘n-1’ enter objects participated in constructing the sum ‘q–xn’, after which we added the final merchandise ‘xn’, and resulted with the mandatory sum ‘q’. So the final merchandise ‘xn’ was really used, and we should construct now one other goal sum ‘q–xn’, by selecting between solely the primary ‘n-1’ enter objects.

    This judgement solutions whether or not the final merchandise ‘xn’ participates within the goal sum ‘q’. However the identical logic additionally works for determining the participation of each different enter merchandise ‘xi’, in another goal sum ‘p’. Recalling the final method by which your complete desk was crammed:

    [begin{equation*}
    S[i][p] = S[i-1][p-x_i] or S[i-1][p]
    finish{equation*}]

    an analogous judgement will be made within the case if any “S[i][p] == true”, which is able to assist us to know if the merchandise ‘xi’ participates in making sum ‘p’, or not.

    So to reconstruct the entire subset ‘Y’, we simply apply this precept repeatedly. The pseudo-code of retrieving ‘Y’ turns into:

    // Given the crammed matrix ‘S’, returns an actual subset of 
    // the n-long set ‘X’, integers of which sum as much as ‘q’.
    operate obtain_subset( 
    		X[1..n]: set of Integers,
    		n: Integer, 
    		q: Integer, 
    		S[0..n][0..q]: Matrix of Booleans ) : Set of Integers
    	if S[n][q] == false then
    		return ∅  // ‘q’ can’t be obtained
    	Y := empty set of Integers
    	whereas n > 0
    		// Right here we all know that “S[n][q]” is at all times true
    		if S[n-1][q] == true then
    			// Merchandise ‘x[n]’ will be not utilized in ‘Y’
    		else
    			// Merchandise ‘x[n]’ ought to be utilized in ‘Y’
    			insert X[n] into Y
    			q := q-X[n]  // Stays to construct the sum ‘q-X[n]’
    		// Transfer upwards to the previous merchandise
    		n := n-1
    	return Y

    Time complexity of this operate is O(n), as on each iteration we transfer upwards by one row over the desk, whereas its peak is ’n+1’.

    In our instance, reconstructing the outcome subset ‘Y’ is carried out by the next path:

    X = {9, 4, 3, 12, 5},
    q = 30.

    Step 1: Right here we have now ‘S[n][q] = S[5][30] = true’ (rightmost inexperienced circle), which signifies that the goal sum ‘q=30’ is reachable. We see that ‘S[4][30] = false’ (blue circle above), so the sum ‘30’ can’t be obtained if selecting between the primary 4 objects. So, we want the merchandise ‘x5=5’ for building.

    Step 2: It stays to assemble the sum ‘30-5=25’, by selecting between the primary 4 objects (2nd inexperienced circle). We see that ‘S[3][25] = false’ (blue circle), which signifies that ‘25’ can’t be constructed if selecting solely between the primary 3 objects. So we have to use the merchandise ‘x4=12’.

    Step 3: It stays to assemble the sum ‘25-12=13’, by selecting between the primary 3 objects (third inexperienced circle). We see that ‘S[2][13] = true’ (4th inexperienced circle), which signifies that ‘13’ will also be gathered by selecting between solely the primary 2 objects. That’s why we skip merchandise ‘x3=3’.

    Step 4: Now we must always assemble the sum ‘13’, by selecting between the primary 2 objects. We see that ‘S[1][13] = false’ (blue circle), which signifies that we are able to’t collect the sum ‘13’ if utilizing solely the primary merchandise ‘x1’. So we have to use ‘x2=4’ for that.

    Step 5: It stays to assemble the sum ‘13-4=9’, by selecting solely the primary enter merchandise (fifth inexperienced circle). So we choose it up, as ‘x1=9’.

    Can we act equally within the Interval-based answer? We see that the one factor wanted to reconstruct subset ‘Y’ within the Dynamic programming answer is realizing the worth of cell “S[i][p]”, which, in relation to the Interval-based answer, is realizing whether or not the i-th sequence of intervals covers the coordinate ‘p’. That may be checked by operating a Binary search over the present i-th sorted sequence of intervals.

    The interval-based answer for a similar sequence of enter values X = {9, 4, 3, 12, 5}. The trail to reconstruct subset ‘Y’ stays the identical, and the intervals that may assist in reconstruction are highlighted in yellow.

    Assuming that the i-th sequence accommodates ‘ci’ intervals, binary search will take O(log ci) time. To retrieve the entire subset ‘Y’, we run binary search on every of the ‘n’ sequences of intervals. If there are at most ‘c’ intervals on every row, retrieval of the outcome subset ‘Y’ will run in O(n log c) time. Methods like Fractional cascading can in all probability be utilized right here to hurry up the subset retrieval course of.


    6. Time complexity of the Interval-based algorithm

    In Chapter 4, we derived the time complexity of the Interval-based algorithm as O(nc), the place ‘n’ is the variety of enter values and ‘c’ is the maximal variety of intervals on a row. We additionally famous there that the order of enter values “X = {x1, x2, …, xn}” issues considerably, and ‘c’ usually leads to a smaller worth when objects of ‘X’ arrive in an growing order. Now, are there such enter circumstances for which the worth ‘c’ will likely be actually small?

    Case 1: A geometrical development

    First let’s take into account the case when values of ‘X’ kind a geometrical development with preliminary worth of ‘1’ and a standard ratio of ‘2’:

    [begin{equation*}
    X = { 1, 2, 4, 8, 16, …, 2^{n-1} } : x_i = 2^{i-1}
    end{equation*}]

    What form can have the set of constructed intervals?

    As we already know, having intervals of the earlier row ‘i-1’, to calculate intervals of the present row ‘i’, there are 2 steps:

    • offset all intervals of row ‘i-1’ by ‘xi’ to the proper,
    • unite the offsets with unique content material of row ‘i-1’.

    Doing that on the talked about enter will end in:

    • row 0 at all times has just one interval [0, 1), stating that only the sum ‘0’ is achievable if using no input item at all.
    • as ‘x1=1’, the shifted interval becomes “[0,1) >> 1 = [1,2)”, and after uniting with the original one we have: “[0,1) ꓴ [1,2) = [0,2)”,
    • as ‘x2=2’, the shifted interval becomes “[0,2) >> 2 = [2,4)”, and after uniting with the original one we have: “[0,2) ꓴ [2,4) = [0,4)”,
    • as ‘x3=4’, the shifted interval becomes “[0,4) >> 4 = [4,8)”, and after uniting with the original one we have: “[0,4) ꓴ [4,8) = [0,8)”,
    • and so on…

    We see that each row ‘i’ has just 1 interval: [0,2i).

    X = {1, 2, 4, 8, 16, …}

    If the input values of ‘X’ form the geometric progression X = {1, 2, 4, 8, 16, …}, every row of the table will have just one interval.

    Of course, the presented case is very special, and it is not a surprise that any target sum ‘q ∈ [0, 2n)’ can be constructed from those ‘n’ numbers. If representing ‘q’ in the binary numeral positional system, then its ‘1’ digits will correspond to the powers of ‘2’ (the input values), which should be added up to get ‘q’.

    Case 2: Slower than a geometric progression

    Values grow rapidly in any geometric progression. In the previous case, as the common ratio was equal to 2, we could also express every value ‘xi’ as the sum of all previous values, plus 1:

    [begin{equation*}
    x_i = 1 + sum_{k=1}^{i-1} x_k
    end{equation*}]

    What if the sequence ‘X’ grows extra slowly? In different phrases, what if after sorting all values of ‘X’ in an growing order, every worth ‘xi’ seems lower than or equal to the sum of all earlier values, plus 1?

    [begin{equation*}
    hspace{1cm} x_i leq 1 + sum_{k=1}^{i-1} x_k hspace{1cm} (1)
    end{equation*}]

    To know how the intervals will likely be derived in such a case, let’s recall from Chapter 4 that the span of row ‘i’ equals the sum of all earlier and the present enter values, plus 1:

    [begin{equation*}
    span(i) = 1 + sum_{k=1}^{i} x_k
    end{equation*}]

    Now, if each worth ‘xi’ is lower than or equal to the sum of all earlier values, it signifies that ‘xi’ can also be lower than the span of its earlier row:

    [begin{equation*}
    x_i leq 1 + sum_{k=1}^{i-1} x_k = span(i-1)
    end{equation*}]

    which signifies that if there is just one interval at row ‘i-1’, uniting it with its offset rightwards by ‘xi’, will nonetheless end in just one interval at row ‘i’. Initially, we have now just one interval in row ‘i=0’. So, within the case when enter values develop extra slowly than the talked about geometric development (1), every row of the desk can have just one interval.

    X = {1, 2, 3, 6, 11, 20},

    When the enter values develop slower than the geometric development with a standard ratio of ‘2’, we nonetheless have just one interval per row.

    Concluding this case, if after sorting all of the enter values of ‘X’ more and more, we have now the constraint:

    [begin{equation*}
    x_i leq 1 + sum_{k=1}^{i-1} x_k
    end{equation*}]

    the desk can have solely “c=1” interval at each row, and the Interval-based algorithm itself will run in assured O(n) time. Let’s word that from the sensible perspective, such a constraint would possibly typically be glad. If the enter knowledge is available in random order, then an extra O(n log n) time will likely be required to kind it.

    Case 3: Nearly slower than a geometrical development

    Let’s generalize the earlier case 2, which had the constraint:

    [begin{equation*}
    x_i leq 1 + sum_{k=1}^{i-1} x_k
    end{equation*}]

    Now we are going to permit for it to be violated every so often.

    As soon as that occurs for some ‘xi’, we are able to count on that the variety of intervals ‘c’ in row ‘i’ will develop into better. However how lengthy can ‘c’ develop? Let’s observe it on the next instance:

    n = 7,
    X = { 1, 2, 5, 7, 10, 28, 30 },

    Will probably be simpler for us to write down down one other array ‘XP’, the place ‘xpi’ is the same as the sum of all previous values:

    XP = { 0, 1, 3, 8, 15, 25, 53, 83 }

    … we see that the talked about situation is violated on the enter values ‘x3=5’ (as ‘x3>xp3+1’) and ‘x6=28’ (as ‘x6>xp6+1’).

    Now let’s assemble the intervals. This time, because the span of all of the inputs is giant (“span(n) = 83+1 = 84”), for the final 2 rows, we are going to write down the sequences of intervals, as a substitute of presenting them geometrically:

    As we are able to see, if for some ‘xi’ the talked about situation is violated, the variety of intervals is doubled on that row. In our instance, that occurred for enter values ‘x3’ and ‘x6’. It occurs as a result of all of the shifted intervals seem rightwards from the span of the present intervals:

    If the present merchandise xi is larger than the sum of all earlier objects plus 1, its shifted intervals (top-right sequence) won’t have any frequent level with the present intervals (top-left sequence), which is why after uniting these two sequences, the variety of intervals will likely be doubled (backside sequence).

    Nonetheless, on the opposite rows, the variety of intervals tends to not enhance a lot, as a result of lots of them, after being united with the present row’s content material, simply flip into one lengthy interval. In our instance, that explicitly occurred on the final row ‘x7=30’.

    If the present merchandise xi is lower than or equal to the sum of all earlier objects plus 1, its shifted intervals (top-right sequence) may need many frequent fragments with the present intervals (top-left sequence), which is why after uniting these two sequences (the underside sequence), a lot of intervals is perhaps merged into one. Right here 6 intervals are being merged into one lengthy interval on the bottom-center.

    The naming of this text comes from the truth that if all enter objects “X = {x1, x2, …, xn}” are shut sufficient to one another, many intervals will are inclined to unite in the course of the top-down building of the desk, so the fixed ‘c’ would possibly stay bounded with sure likelihood. If that’s the case, we are going to find yourself with “O(nc) = O(n)” linear time answer for the Subset Sum Drawback, assuming that the enter values arrive in a sorted manner. In any other case, the preliminary sorting of values of ‘X’ turns into probably the most time-consuming step, requiring further “O(n log n)” time.

    Case 4: Sooner than a geometrical development

    Let’s take into account yet another case, when the sorted enter values
    “X = {x1, x2, …, xn}” develop quicker than the talked about geometric development with a standard ratio of two. That can occur if each worth ‘xi’ is larger than the sum of all earlier values, plus 1:

    [begin{equation*}
    hspace{1cm} x_i > 1 + sum_{k=1}^{i-1} x_k hspace{1cm} (2)
    end{equation*}]

    Within the earlier case 3, we already famous that when it occurs for some ‘xi‘ (i.e., when ‘xi’ seems rightwards from the span of all earlier values), the variety of intervals doubles, as a result of no frequent fragments stay between the present and the shifted sequences of intervals.

    And within the present case, when ‘X’ grows quicker than a geometrical development with a standard ratio of two, it occurs for each enter worth ‘xi’, so the variety of intervals doubles on each row, leading to an exponential progress.

    Let’s take into account the next instance:

    n = 6,
    X = { 2, 5, 9, 20, 39 }.

    The array with sums of all previous values will likely be:

    XP = { 0, 2, 7, 16, 36, 75 }.

    So we see that each merchandise ‘xi’ is larger than the sum of all earlier objects, plus 1. Establishing intervals will outcome within the following desk:

    So we see that if enter values “X” have the talked about constraint (2), continuing with the interval-based algorithm isn’t the very best thought, as it can end in 2n quick intervals on the final row, thus requiring O(2n) time to run. If we all know prematurely that this will likely be our case, one other decision-based algorithm will be utilized, which is able to choose a needed subset of “X” in linear time O(n).


    Conclusion

    On this article, we have now noticed a novel strategy for fixing the Subset Sum Drawback, referred to as “Interval-based algorithm”. It’s just like the Dynamic Programming answer, with the distinction that right here we function not on particular person cells of the desk, however on its steady ranges.

    Now we have noticed 4 particular distributions of enter values, and proved that if the inputs are dense sufficient, the Interval-based algorithm runs in linear time. Now we have additionally proven that when the inputs are “nearly dense”, the algorithm would possibly nonetheless work quick sufficient. Just like the Dynamic Programming answer, the Interval-based algorithm permits acquiring the precise subset, objects of which sum as much as the given goal.

    The Subset Sum Drawback is without doubt one of the NP-complete issues; thus, this text highlights one other particular case of inputs, for which they are often solved quick sufficient.

    I’m comfortable that you’ve got learn until the top, and thanks to your curiosity!


    All of the used illustrations are ready by Lilit Danielyan ( https://www.behance.net/lilitdanielyan1 ).

    In case you loved studying, be happy to attach with me on LinkedIn, and DM to share your ideas. I will surely prefer it! ( https://www.linkedin.com/in/tigran-hayrapetyan-cs/ ).

    All used photographs, except in any other case famous, are designed by request of the creator.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Editor Times Featured
    • Website

    Related Posts

    KV Cache Is Eating Your VRAM. Here’s How Google Fixed It With TurboQuant.

    April 19, 2026

    Proxy-Pointer RAG: Structure Meets Scale at 100% Accuracy with Smarter Retrieval

    April 19, 2026

    Dreaming in Cubes | Towards Data Science

    April 19, 2026

    AI Agents Need Their Own Desk, and Git Worktrees Give Them One

    April 18, 2026

    Your RAG System Retrieves the Right Data — But Still Produces Wrong Answers. Here’s Why (and How to Fix It).

    April 18, 2026

    Europe Warns of a Next-Gen Cyber Threat

    April 18, 2026

    Comments are closed.

    Editors Picks

    AI evolves itself to speed up scientific discovery

    April 20, 2026

    Australia’s privacy commissioner tried, in vain, to sound the alarm on data protection during the u16s social media ban trials

    April 20, 2026

    Nothing Phone (4a) Pro Review: A Close Second

    April 20, 2026

    Match Group CEO Spencer Rascoff says growing women’s share on Tinder is his “primary focus” to stem user declines; Sensor Tower says 75% of Tinder users are men (Kieran Smith/Financial Times)

    April 20, 2026
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    About Us
    About Us

    Welcome to Times Featured, an AI-driven entrepreneurship growth engine that is transforming the future of work, bridging the digital divide and encouraging younger community inclusion in the 4th Industrial Revolution, and nurturing new market leaders.

    Empowering the growth of profiles, leaders, entrepreneurs businesses, and startups on international landscape.

    Asia-Middle East-Europe-North America-Australia-Africa

    Facebook LinkedIn WhatsApp
    Featured Picks

    OpenAI sidesteps Nvidia with unusually fast coding model on plate-sized chips

    February 13, 2026

    Research Reveals the Optimal Way to Optimize

    December 21, 2025

    I get trolled every day for having fewer viewers

    September 19, 2025
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    Copyright © 2024 Timesfeatured.com IP Limited. All Rights.
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us

    Type above and press Enter to search. Press Esc to cancel.