Generic triangular numbers sequence formula - algorithm

I know that I can get the nth element of the following sequence
1 3 6 10 15 21
With the formula
(n * (n + 1)) / 2
where n is the nth number I want. How can I generalise the formula to get the nth element of the following sequences where by following sequences I mean
1 -> 1 3 6 10 15 21
2 -> 2 5 9 14 20
3 -> 4 8 13 19
4 -> 7 12 18
5 -> 11 17
6 -> 16

It is not quite clear what do you mean by n-th element in 2D-table (potentially infinite)
Simple formula for element at row and column (numbered from 1):
(r+c-1)*(r+c)/2 - (r-1)
Possible intuition for this formula:
Key moment: element with coordinates r,c stands on the diagonal number d, where d = r + c - 1
There are s = d*(d+1)/2 elements in d filled diagonals, so the last element of d-th diagonal (rightmost top) has value s, and element in r-th row of the same diagonal is
v(r,c) = s-(r-1) = (d)*(d+1)/2 -(r-1) = (r+c-1)*(r+c)/2 - (r-1)

Related

formula for index of a children of a node in a d-ary heap

Assume we have a d-ary heap and we index the nodes from top to bottom and from left to right(starting with 1). Then the children from node i are the nodes with index di,...,di+(d-1). I read this formula in a couple of books but in none of them were an explanation why these formulas are true. Maybe I am overlooking something but is it really that clear that these formulas are true?
I find d * i + 2 - d for the index of the first child, if items
are numbered starting from 1. Here is the reasoning
Each row contains the children of the previous row. If n[r] are
the number of items on row r, one must have n[r+1] = d * n[r], which
proves that n[r] = d**r if the first row is numbered 0. The index
of the first item of row r is f[r] = 1 + (d**r - 1)/(d - 1) by the sum
of geometric sequences. If item X with number i is on row r, let's write
i = f[r] + k with 0 <= k < d**r. There are k items on the row before X,
hence there are d * k items before X's first child on row r+1. The
index of X's first child is f[r+1] + d * k = f[r+1] + d * (i - f[r])
The calculus gives d * i + 2 - d for the index of the first child.
Actually, if we start numbering the items from 0 instead of 1, the formula becomes simply d * i + 1 for the index of the first child, and this can be easily proven by induction because the index of the first child of item i+1 is obtained by adding d, but (d * i + 1) + d = d * (i + 1) + 1.
Maybe this diagram will help, at least for d=2:
1
2 3
4 5 6 7
8 9 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Printing a 2D pattern

I discovered this pattern and decided to print it.
1 2 5 10 17
3 4 7 12 19
6 8 9 14 21
11 13 15 16 23
18 20 22 24 25
Rule here is to move from (0,0) to (0,1) to (1,0) to (1,1) to (0,2) to (2,0) to (1,2) to (2,1) to (2,2) and so on upto NxN matrix.
I have a very complex approach for printing it. Is there any easy way to print this pattern?
Update: Added one more row and column
It seems like the general rule is as follows:
Given a position as a tuple (n, m), the next position is
(n+1, 0), if n = m
(m, n), if n > m
(m, n+1), if n < m
In Python:
def next_pos(n, m):
if n == m: return (n+1, 0)
if n > m: return (m, n)
if n < m: return (m, n+1)
Example:
N = 5
n, m = (0, 0)
matrix = [[None for _ in range(N)] for _ in range(N)]
for x in range(N*N):
matrix[m][n] = x+1
n, m = next_pos(n, m)
Result:
1 2 5 10 17
3 4 7 12 19
6 8 9 14 21
11 13 15 16 23
18 20 22 24 25
Here is a Python solution that first extends every row by every other number starting with 1 more than the last perfect square, and then adds a new row, which consists of every other number starting with 2 more than the last perfect square, along with the final entry (which is the next perfect square in the sequence):
def expandSquare(square):
n = len(square[0])
for i, row in enumerate(square):
row.append(n**2 + 1 + 2*i)
square.append([k for k in range(n**2 + 2, (n+1)**2,2)] + [(n+1)**2])
def makeSquare(n):
square = [[1]]
for i in range(1,n): expandSquare(square)
return square
def pprint(square):
print('\n'.join('\t'.join(str(i) for i in row) for row in square))
For example,
>>> pprint(makeSquare(5))
1 2 5 10 17
3 4 7 12 19
6 8 9 14 21
11 13 15 16 23
18 20 22 24 25

Fastest way to find sum of any rectangle in matrix

I have a m x n matrix and want to be able to calculate sums of arbitrary rectangular submatrices. This will happen several times for the given matrix. What data structure should I use?
For example I want to find sum of rectangle in matrix
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Sum is 68.
What I'll do is accumulating it row by row:
1 2 3 4
6 8 10 12
15 18 21 24
28 32 36 40
And then, if I want to find sum of the matrix I just accumulate 28,32,36,40 = 136. Only four operation instead of 15.
If I want to find sum of second and third row, I just accumulate 15,18,21,24 and subtract 1, 2, 3, 4. = 6+8+10+12+15+18+21+24 = 68.
But in this case I can use another matrix, accumulating this one by columns:
1 3 6 10
5 11 18 26
9 19 30 42
13 27 42 58
and in this case I just sum 26 and 42 = 68. Only 2 operation instead of 8. For wider sub-matrix is is efficient to use second method and matrix, for higher - first one. Can I somehow split merge this to methods to one matrix?
So I just sum to corner and subtract another two?
You're nearly there with your method. The solution is to use a summed area table (aka Integral Image):
http://en.wikipedia.org/wiki/Summed_area_table
The key idea is you do one pass through your matrix and accumulate such that "the value at any point (x, y) in the summed area table is just the sum of all the pixels above and to the left of (x, y), inclusive.".
Then you can compute the sum inside any rectangle in constant time with four lookups.
Why can't you just add them using For loops?
int total = 0;
for(int i = startRow; i = endRow; i++)
{
for(int j = startColumn; j = endColumn; j++)
{
total += array[i][j];
}
}
Where your subarray ("rectangle") would go from startRow to endRow (width) and startColumn to endColumn (height).

Find two subarrays with equal weighted average

We are given an array A of integers. I want to find 2 contiguous subarrays of the largest length(both subarrays must be equal in length) that have the same weighted average. The weights are the positions in the subarray. For example
A=41111921111119
Subarrays:: (11119) and (11119)
Ive tried to find the weighted average of all subarrays by DP and then sorting them columnwise to find 2 with same length.But I cant proceed further and my approach seems too vague/bruteforce.I would appreciate any help. Thanks in advance.
The first step should be to sort the array. Any pairs of equal values can then be identified and factored out. The remaining numbers will all be different, like this:
2, 3, 5, 9, 14, 19 ... etc
The next step would be to compare pairs to their center:
2 + 5 == 2 * 3 ?
3 + 9 == 2 * 5 ?
5 + 14 == 2 * 9 ?
9 + 19 == 2 * 14 ?
The next step is to compare nested pairs, meaning if you have A B C D, you compare A+D to B+C. So for the above example it would be:
2+9 == 3+5 ?
3+15 == 5+9 ?
5+19 == 9+14 ?
Next you would compare triples to the two inside values:
2 + 3 + 9 == 3 * 5 ?
2 + 5 + 9 == 3 * 3 ?
3 + 5 + 14 == 3 * 9 ?
3 + 9 + 14 == 3 * 5 ?
5 + 9 + 19 == 3 * 14 ?
5 + 14 + 19 == 3 * 9 ?
Then you would compare pairs of triples:
2 + 3 + 19 == 5 + 9 + 14 ?
2 + 5 + 19 == 3 + 9 + 14 ?
2 + 9 + 19 == 3 + 5 + 14 ?
and so on. There are different ways to do the ordering. One way is to create an initial bracket, for example, given A B C D E F G H, the initial bracket is ABGH versus CDEF, ie the outside compared to the center. Then switch values according to the comparison. For example, if ABGH > CDEF, then you can try all switches where the left value is greater than the right value. In this case G and H are greater than E and F, so the possible switches are:
G <-> E
G <-> F
H <-> E
H <-> F
GH <-> EF
First, as the length of the two subarray must be equal, you can consider the length from 1 to n step by step.
For length i, you can calculate the weighted sum of every subarray in a total complexity of O(n). Then sort the sums to determine if there's an equal pair.
Because you sort n times the time would be O(n^2 log n) while the space is O(n).
Maybe I just repeated your solution mentioned in the question? But I don't think it can be optimized any more...

Summation of difference between matrix elements

I am in the process of building a function in MATLAB. As a part of it I have to calculate differences between elements in two matrices and sum them up.
Let me explain considering two matrices,
1 2 3 4 5 6
13 14 15 16 17 18
and
7 8 9 10 11 12
19 20 21 22 23 24
The calculations in the first row - only four elements in both matrices are considered at once (zero indicates padding):
(1-8)+(2-9)+(3-10)+(4-11): This replaces 1 in initial matrix.
(2-9)+(3-10)+(4-11)+(5-12): This replaces 2 in initial matrix.
(3-10)+(4-11)+(5-12)+(6-0): This replaces 3 in initial matrix.
(4-11)+(5-12)+(6-0)+(0-0): This replaces 4 in initial matrix. And so on
I am unable to decide how to code this in MATLAB. How do I do it?
I use the following equation.
Here i ranges from 1 to n(h), n(h), the number of distant pairs. It depends on the lag distance chosen. So if I choose a lag distance of 1, n(h) will be the number of elements - 1.
When I use a 7 X 7 window, considering the central value, n(h) = 4 - 1 = 3 which is the case here.
You may want to look at the circshfit() function:
a = [1 2 3 4; 9 10 11 12];
b = [5 6 7 8; 12 14 15 16];
for k = 1:3
b = circshift(b, [0 -1]);
b(:, end) = 0;
diff = sum(a - b, 2)
end

Resources