Find pattern in three sequences of numbers

178 Views Asked by At

I am developing an algorithm to fill a matrix in a column-wise fashion. This means in a loop I iterate by column, and then by row.

For an input set:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

I want to fill the lower triangular matrix, i.e.:

[0,  -,  -,  -,  -]
[1,  0,  -,  -,  -]
[2,  5,  0,  -,  -]
[3,  6,  8,  0,  -]
[4,  7,  9, 10,  0]

But I am stuck into finding the mathematical relationship between three sequence of numbers. It's easier to explain with a simplified example with expected outputs:

My input set S has 10 elements. I will be iterating this set.

  • When I iterate the sequence 1 : 1, 2, 3, 4

  • I need two sequences: 2, 3, 4, 5 and column index 1, 1, 1, 1

  • For sequence 2 : 5, 6, 7

  • I need sequences: 3, 4, 5 and 2, 2, 2

  • For sequence 3 : 8, 9

  • I need sequences: 4, 5 and 3, 3

  • For sequence 4 : 10

  • I need sequences: 5 and 4

Since I am iterating by column, for any iteration with column_index value I know to obtain the multiset of repetitions (for example: 1,1,1,1), I need to repeat the ((size of set S) / 2) - column_index.

But I think I am overcomplicating this and I suspect there is a pattern or formulas I can't see here.

1

There are 1 best solutions below

0
On

For sequence 1: the difference between each element of (2,3,4,5) and that of the required sequence (1,2,3,4) = +1

For sequence 2: the difference between each element of (3,4,5) and (5,6,7) = -2

For sequence 3: the difference between each element of (4,5) and (8,9) = -4

For sequence 4: the difference between each element of (5) and (10)=-5

Notice the pattern in the differences: +1, -2, -4, -5. They are decreasing by 3,2,1 respectively.

(ie) 1-3=-2

-2-2=-4

-4-1=-5

Now you can use an iterating variable that would decrease by 1 on each iteration. I hope that this would be helpful for you!