<p>You can solve this with dynamic programming as well.</p>

<p>To see this, first realize you can see this problem as splitting the numbers between $0$ and $N$ into two subsets $X$ and $Y$ (the cumulative sums) that cannot overlap except at $0$ and $N$ (and must overlap there), and where the difference between consecutive elements of a subset is at most 3.</p>

<p>Reformulating the problem this way, you can have the state of your dynamic programming be:</p>

<p>"The number of ways of splitting the numbers between $0$ and $i-1$ into two sequences in the fashion described in the second paragraph, where the the numbers in positions (i, i-1, i-2) have assignments (j,k,h)", with $3 \leq i \leq N-1$, and $(j,k,h) \in \{X,Y,None\}$.</p>

<p>Using the states for $i=k$ to compute the states for $i=k+1$, it does take $\Theta(N)$ time to compute this. Let me know if more details are required.</p>

<p>Edit: I had missed in the previous answer the fact that $T$ and $U$ are fixed. You can take care of this by adding in your state how many positions so far have assignment to $X$, and how many have assignments to $Y$. Sadly, this brings the complexity up to $\Theta(T*U*N)$. Also, note that the solution generalizes to a larger number of sequences, and their values being in a larger range $\{1, \ldots, k \}$.</p>

CC BY-SA 3.0
2013-03-13T15:47:33.637Z
Hi Abel, Sorry for taking so long to respond to your answer. Can you post the recursive solution to the problem? I'm having trouble understanding how solving the problem for N - 1 helps in solving the problem for N, since at N we know that the cumulative sums will be equal, but at N - 1 they cannot be, so how why is N - 1 a problem instance?
Alexandre
CC BY-SA 3.0
2013-03-16T19:32:38.800Z
The main problem is not one of the subproblems, precisely because we have the extra condition that N is assigned to X and Y. However, the answer to the problem is the number of ways of assigning integers in [1, N-1] as cumulative sums to A and B, in such a way that T-1 of them belong to A, and U-1 belong to B. So, the answer to the problem is the sum over all states with N-1, T-1, L-1.
Abel Molina
CC BY-SA 3.0
2013-03-02T07:24:46.090Z
2013-03-12T18:09:06.427Z
2013-03-12T18:09:06.427Z
Abel Molina
<p>This problem has an algorithm of time complexity that is polynomial in N, T and U (but not in polynomial in the size of N, T and U). This technicality is worth noting since the problem instances can be described by specifying just the integers T, U and N and hence strictly speaking the size of the input to this problem is O(log N + log T + log U). So in order to get a polynomial time algorithm, you should assume that the inputs are given in unary.</p>

<p>One can create an acyclic graph G with O(T*U*N) nodes with source s and sink t such that the number of paths from s to t is exactly the solution.  </p>

CC BY-SA 3.0
2013-03-03T01:21:49.370Z
2013-03-03T01:21:49.370Z
user14004
<p>This question arose from a discussion between a friend and I. </p>

<p>$A$ is a sequence of length $T$ where for any $a_i$ in $A$,  $a_i \in  \left\{{1, 2, 3}\right\}$</p>

<p>$B$ is a sequence of length $U$ where for any $b_i$ in $B$,  $b_i \in  \left\{{1, 2, 3}\right\}$</p>

<hr>

<p>And the following conditions:</p>

<p>1) They possess equal sum,</p>

<p>$N = \sum_{\forall a \in A} a = \sum_{\forall b \in B} b$</p>

<hr>

<p>2)  They don't possess sub-sequences starting at the head of equal sum,</p>

<p>$\sum_{i=1}^c a_i \neq \sum_{i=1}^d b_i$ </p>

<p>for any $c &lt; T$ and $d &lt; U$, </p>

<p>where $a_i \in A$ and $b_i \in B$</p>

<hr>

<p><strong>Question: For given $N$, find an algorithm that calculates in polynomial time the number of possible pairs $(A, B)$ that satisfy these conditions.</strong></p>

<p>It's very easy to write an algorithm that satisfies condition 1) using dynamic programming, but I simply cannot figure out how to make it satisfy 2) as well.  </p>

<p>Good luck!</p>

<dynamic-programming>
Find two sequences of integers that have sum N but that don't have sub-sequences starting at the head of equal sum
Alexandre
# Find two sequences of integers that have sum N but that don't have sub-sequences starting at the head of equal sum

(Asked by: username_0 on 2013-03-01)

This question arose from a discussion between a friend and I.

$A$ is a sequence of length $T$ where for any $a\_i$ in $A$, $a\_i \in \left\{{1, 2, 3}\right\}$

$B$ is a sequence of length $U$ where for any $b\_i$ in $B$, $b\_i \in \left\{{1, 2, 3}\right\}$

---

And the following conditions:

1) They possess equal sum,

$N = \sum\_{\forall a \in A} a = \sum\_{\forall b \in B} b$

---

2) They don't possess sub-sequences starting at the head of equal sum,

$\sum\_{i=1}^c a\_i \neq \sum\_{i=1}^d b\_i$

for any $c < T$ and $d < U$,

where $a\_i \in A$ and $b\_i \in B$

---

**Question: For given $N$, find an algorithm that calculates in polynomial time the number of possible pairs $(A, B)$ that satisfy these conditions.**

It's very easy to write an algorithm that satisfies condition 1) using dynamic programming, but I simply cannot figure out how to make it satisfy 2) as well.

Good luck!

**Tags:** dynamic-programming

---

## 2 answers

### Answer by username_1 on 2013-03-02. Score: 4

You can solve this with dynamic programming as well.

To see this, first realize you can see this problem as splitting the numbers between $0$ and $N$ into two subsets $X$ and $Y$ (the cumulative sums) that cannot overlap except at $0$ and $N$ (and must overlap there), and where the difference between consecutive elements of a subset is at most 3.

Reformulating the problem this way, you can have the state of your dynamic programming be:

"The number of ways of splitting the numbers between $0$ and $i-1$ into two sequences in the fashion described in the second paragraph, where the the numbers in positions (i, i-1, i-2) have assignments (j,k,h)", with $3 \leq i \leq N-1$, and $(j,k,h) \in \{X,Y,None\}$.

Using the states for $i=k$ to compute the states for $i=k+1$, it does take $\Theta(N)$ time to compute this. Let me know if more details are required.

Edit: I had missed in the previous answer the fact that $T$ and $U$ are fixed. You can take care of this by adding in your state how many positions so far have assignment to $X$, and how many have assignments to $Y$. Sadly, this brings the complexity up to $\Theta(T\*U\*N)$. Also, note that the solution generalizes to a larger number of sequences, and their values being in a larger range $\{1, \ldots, k \}$.

> **Comments:**
> **username_0**: Hi Abel, Sorry for taking so long to respond to your answer. Can you post the recursive solution to the problem? I'm having trouble understanding how solving the problem for N - 1 helps in solving the problem for N, since at N we know that the cumulative sums will be equal, but at N - 1 they cannot be, so how why is N - 1 a problem instance?  
> **username_1**: The main problem is not one of the subproblems, precisely because we have the extra condition that N is assigned to X and Y. However, the answer to the problem is the number of ways of assigning integers in [1, N-1] as cumulative sums to A and B, in such a way that T-1 of them belong to A, and U-1 belong to B. So, the answer to the problem is the sum over all states with N-1, T-1, L-1.  

---

### Answer by username_2 on 2013-03-03. Score: 2

This problem has an algorithm of time complexity that is polynomial in N, T and U (but not in polynomial in the size of N, T and U). This technicality is worth noting since the problem instances can be described by specifying just the integers T, U and N and hence strictly speaking the size of the input to this problem is O(log N + log T + log U). So in order to get a polynomial time algorithm, you should assume that the inputs are given in unary.

One can create an acyclic graph G with O(T\*U\*N) nodes with source s and sink t such that the number of paths from s to t is exactly the solution.


---

