0

I am trying to find a dynamic approach to multiply each element in a linear sequence to the following element, and do the same with the pair of elements, etc. and find the sum of all of the products. Note that any two elements cannot be multiplied. It must be the first with the second, the third with the fourth, and so on. All I know about the linear sequence is that there are an even amount of elements.

I assume I have to store the numbers being multiplied, and their product each time, then check some other "multipliable" pair of elements to see if the product has already been calculated (perhaps they possess opposite signs compared to the current pair).

However, by my understanding of a linear sequence, the values must be increasing or decreasing by the same amount each time. But since there are an even amount of numbers, I don't believe it is possible to have two "multipliable" pairs be the same (with potentially opposite signs), due to the issue shown in the following example:

Sequence: { -2, -1, 0, 1, 2, 3 }

Pairs: -2*-1, 0*1, 2*3

Clearly, since there are an even amount of pairs, the only case in which the same multiplication may occur more than once is if the elements are increasing/decreasing by 0 each time.

I fail to see how this is a dynamic programming question, and if anyone could clarify, it would be greatly appreciated!

user3495690
  • 576
  • 3
  • 15

2 Answers2

0

A quick google for define linear sequence gave

A number pattern which increases (or decreases) by the same amount each time is called a linear sequence. The amount it increases or decreases by is known as the common difference.

In your case the common difference is 1. And you are not considering any other case.

The same multiplication may occur in the following sequence

Sequence = {-3, -1, 1, 3} Pairs = -3 * -1 , 1 * 3

with a common difference of 2.

However this is not necessarily to be solved by dynamic programming. You can just iterate over the numbers and store the multiplication of two numbers in a set(as a set contains unique numbers) and then find the sum.

Abhisek
  • 4,610
  • 3
  • 17
  • 27
0

Probably not what you are looking for, but I've found a closed solution for the problem.

Suppose we observe the first two numbers. Note the first number by a, the difference between the numbers d. We then count for a total of 2n numbers in the whole sequence. Then the sum you defined is:

sum = na^2 + n(2n-1)ad + (4n^2 - 3n - 1)nd^2/3

That aside, I also failed to see how this is a dynamic problem, or at least this seems to be a problem where dynamic programming approach really doesn't do much. It is not likely that the sequence will go from negative to positive at all, and even then the chance that you will see repeated entries decreases the bigger your difference between two numbers is. Furthermore, multiplication is so fast the overhead from fetching them from a data structure might be more expensive. (mul instruction is probably faster than lw).

Flying_Banana
  • 2,864
  • 2
  • 22
  • 38