Skip to main content

Maximum Non-Contiguous Sum of a Series

Given an arbitrary series, we've to calculate the maximum sum confined to only one restriction-
If we choose any number a [ i ] to calculate the sum, we can't choose a [ i+1 ]
Let's take a series: a[ ]= {1,2,3,4,5,9}

Linear Solution: Take two variable inc and exl. inc will store value if we choose a [ i ] to calculate the sum & exl will store value if we do not choose a [ i ], for any 0 ≤ i ≤ n. Now for every step, we have to calculate inc and exl. After iteration ends, max( inc,exl ) is the maximum non-contiguous sum. Roughly-

Why inc = exl + a [ i ] ? because the already calculated exl is from i-2. So if we choosed inc=  inc + a [ i ] , that would be a contiguous sum. exl is maximum of inc & exl right until  i-1.
Top Down solution( DP ): If you're familiar with dynamic programming, you know that, we need to calculate dp[n] - the maximum non contiguous sum at nth index.
Now we can have two possible state, we'll select the maximum one.
dp[ i ] = a [ i ] + dp [ i-2 ]   choosing i'th value
dp[ i ] = a [ i-1 ] + dp [ i-3 ]  choosing the value before i'th

Comments

Popular posts from this blog

SPOJ - GSS1

Considering input series: { 4 , -10 , 3 , 100 , -20 , 1 } Query(x,y) = Max { a[i]+a[i+1]+...+a[j] ; x ≤ i ≤ j ≤ y } A node contains-  [ START & END is a node's segment limit ] Prefix is the maximum sum starting at START, end can be anywhere. There are two possibilities of the maximum. One, node's leftChild's prefix or two, adding leftChild's sum + rightChild's prefix. (which will make the prefix contiguous) Suffix is the maximum sum ending at END, start can be anywhere. There's two possibility of the maximum. One, node's rightChild's already calculated suffix or two, add rightChild's sum + leftChild's suffix   (which will make the suffix contiguous). Sum : leftChild's sum + rightChild's sum. MAX Maximum of  -  prefix (result is in the node, starts from START but doesn't end in END ) suffix  (result is in the node, doesn't start from START but surely ends in END ) leftChild's max ( result is in left ...

SPOJ 95 - STPAR - Street Parade

Problem Statement Heuristics:   Let, N be the number which is next in fixed order. Take an input X , if it is equal to N then next number will be n++. If not, then search for N in the side lane and after finishing the search, put X in the side lane. After all X is processed but N is not found in either X or side lane, then the order is not possible. Solution:

SPOJ-PIGBANK | Unbounded Knapsack ( Minimization)

Unbounded Knapsack : Given a knapsack weight W and a set of N items with certain value Vi and weight W i , we need to calculate minimum amount that could make up this quantity exactly. To have a more clear understanding of this problem, see SPOJ-PIGBANK Sample Problem : We're given a box which can hold maximum W weight. There are N various coins each of value Vi and Weight Wi. We need to fill the box with total W weight with any number of coins of any value. Note that coins are unlimited.  100 ----- W 2 ------- N 1 1 ----- vi,wi 30 50 --- vi,wi Answer of the problem above is, 60. If we take 2 coins of value 30, weight 50, we'll get exactly weight 100 with a minimum value of 60. Algorithm : This is a DP solution. To get the solution for W=100, we'll solve subproblems first. Let dp[W+1]  be an array which will store the subproblems solutions. In the end, dp[W] will give us the minimum amount. Subproblems soultion will be the minimum of taking the coin Vk ...