Diagonal number pattern logic - algorithm

I want to device an algorithm to display the following pattern:
1
9 2
10 8 3
14 11 7 4
15 13 12 6 5
Is there a way to convert it into the following array and use the indices of the above matrix and find out the position of the number in the array:
1 9 2 10 8 3 ...
I can't find a pattern to calculate the element using the matrix co-ordinates, that is why I was trying to device the above method of somehow determining the position of the next number in the array.

Just clues:
You need to know formula for sum of arithmetic progression (here sum of natural numbers 1 + 2 + 3 +.. N)
1st step: determine number of diagonal where k-th item of array stands.
2nd step: get direction of this diagonal filling
3rd step: get number of place at this diagonal
4th step: find what number stands here

Related

Find elements of given matrix

You are given an infinite matrix whose upper-left square starts with 1. Here are the first five rows of the infinite matrix :
1 2 9 10 25
4 3 8 11 24
5 6 7 12 23
16 15 14 13 22
17 18 19 20 21
Your task is to find out the number in presents at row x and column y after observing a certain kind of patter present in the matrix
Input Format
The first input line contains an integer t: the number of test cases
After this, there are t lines, each containing integer x and y
For each test, print the number present at xth row and yth column.
sample input
3
2 3
1 1
4 2
sample output
8
1
15
Hint: the numbers at the right and bottom border of a left upper square are consecutive (going either down and left, or right and up). First determine in which border your position is, then find out which direction applies, and finally find the correct number at the position (which easy formula gives you the first number in the border?).

what does 'work through an algorithm by hand' mean?

I'm doing this assignment and I don't understand the wording. Do you think it means to write in pseudocode or write a paragraph? Does anyone have any ideas?
It means to describe the algorithm with words and draw the array values in each step. Here is an example: https://www.geeksforgeeks.org/bubble-sort/
Before executing the algorithm, the array is:
4 2 12 1 7 9 9
After executing the algorithm, the array is:
1 2 4 7 9 9 12
During the execution of the algorithm, the array slowly changes from what is was before, to what it will be after. Your assignment requires you to show all the intermediate steps.
For instance, the very first step of execution will be "compare element at position 0 with position 1; if element at position 1 is lower, then swap the two elements". The first two elements are 4 and 2; 2 is lower; hence they should be swapped; the resulting array is:
2 4 12 1 7 9 9
Then the second step will be "compare elements at position 1 and 2", which are 4 and 12; etc.

Ascending Cardinal Numbers in APL

In the FinnAPL Idiom Library, the 19th item is described as “Ascending cardinal numbers (ranking, all different) ,” and the code is as follows:
⍋⍋X
I also found a book review of the same library by R. Peschi, in which he said, “'Ascending cardinal numbers (ranking, all different)' How many of us understand why grading the result of Grade Up has that effect?” That's my question too. I searched extensively on the internet and came up with zilch.
Ascending Cardinal Numbers
For the sake of shorthand, I'll call that little code snippet “rank.” It becomes evident what is happening with rank when you start applying it to binary numbers. For example:
X←0 0 1 0 1
⍋⍋X ⍝ output is 1 2 4 3 5
The output indicates the position of the values after sorting. You can see from the output that the two 1s will end up in the last two slots, 4 and 5, and the 0s will end up at positions 1, 2 and 3. Thus, it is assigning rank to each value of the vector. Compare that to grade up:
X←7 8 9 6
⍋X ⍝ output is 4 1 2 3
⍋⍋X ⍝ output is 2 3 4 1
You can think of grade up as this position gets that number and, you can think of rank as this number gets that position:
7 8 9 6 ⍝ values of X
4 1 2 3 ⍝ position 1 gets the number at 4 (6)
⍝ position 2 gets the number at 1 (7) etc.
2 3 4 1 ⍝ 1st number (7) gets the position 2
⍝ 2nd number (8) gets the position 3 etc.
It's interesting to note that grade up and rank are like two sides of the same coin in that you can alternate between the two. In other words, we have the following identities:
⍋X = ⍋⍋⍋X = ⍋⍋⍋⍋⍋X = ...
⍋⍋X = ⍋⍋⍋⍋X = ⍋⍋⍋⍋⍋⍋X = ...
Why?
So far that doesn't really answer Mr Peschi's question as to why it has this effect. If you think in terms of key-value pairs, the answer lies in the fact that the original keys are a set of ascending cardinal numbers: 1 2 3 4. After applying grade up, a new vector is created, whose values are the original keys rearranged as they would be after a sort: 4 1 2 3. Applying grade up a second time is about restoring the original keys to a sequence of ascending cardinal numbers again. However, the values of this third vector aren't the ascending cardinal numbers themselves. Rather they correspond to the keys of the second vector.
It's kind of hard to understand since it's a reference to a reference, but the values of the third vector are referencing the orginal set of numbers as they occurred in their original positions:
7 8 9 6
2 3 4 1
In the example, 2 is referencing 7 from 7's original position. Since the value 2 also corresponds to the key of the second vector, which in turn is the second position, the final message is that after the sort, 7 will be in position 2. 8 will be in position 3, 9 in 4 and 6 in the 1st position.
Ranking and Shareable
In the FinnAPL Idiom Library, the 2nd item is described as “Ascending cardinal numbers (ranking, shareable) ,” and the code is as follows:
⌊.5×(⍋⍋X)+⌽⍋⍋⌽X
The output of this code is the same as its brother, ascending cardinal numbers (ranking, all different) as long as all the values of the input vector are different. However, the shareable version doesn't assign new values for those that are equal:
X←0 0 1 0 1
⌊.5×(⍋⍋X)+⌽⍋⍋⌽X ⍝ output is 2 2 4 2 4
The values of the output should generally be interpreted as relative, i.e. The 2s have a relatively lower rank than the 4s, so they will appear first in the array.

Nullify a 2D matrix with some set of operations

Given an N x M matrix having only positive integer values, we have to nullify the matrix
i.e make all entries 0.
We are given two operations
1) multiply each element of any one column at a time by 2.
2) Subtract 1 from all elements of any one row at a time
Find the minimum number of operations required to nullify the matrix.
i thought of doing something related to LCM but could not reach to a solution
Let's first solve for 1 row first and we can extend it to all rows. Let's take a random example:
6 11 5 13
The goal is to make all elements as 1. First we make 5 (smallest element) as 1. For this we need to subtract 4 (i.e subtract 1 four times). The resultant array is:
2 7 1 9
Now we multiply 1 with 2 and subtract all row elements by 1:
1 6 1 8
Next, we multiply 2 1's by 2 and subtract all row elements by 1:
1 5 1 7
Continuing in this manner, we get to 1 1 1 1. Now we subtract 1 to get 0 0 0 0.
Next, we get to other rows and do the same like above. The row we nullified above are all zeroes so multiplication by 2 when manipulating other rows doesn't change the already nullified rows.
The question of finding the minimum number of operations would also depend on the row sequence we select. I think that would be to select a row whose maximum is minimum (among other rows) first. I need to verify this.

maze traversal sum of digits

a grid of NxN is given. each point is assigned a value say num
starting from 1,1 we have to traverse to N,N.
if i,j is current position we can go right or down.
How to find the min sum of digits by traversing from 1,1 to n,n along any path
any two points can have same number
ex
1 2 3
4 5 6
7 8 9
1+2+3+6+9 = 21
n <=10000000000
Output 21
Can someone explain how to approach the problem?
This is a dynamic programming problem. The subproblem here is the minimum cost/path to get to any given square. Because you can only move down and to the right, there are only two squares that can let you enter a given square, the one above and the one to the left. Therefore the cost of getting to a square (i,i) is min(cost[i-1][i], cost[i][i-1]) + num. If this would put you out of bounds, only consider the option that is inside the grid. Calculate each row from left to right, doing the top row first and working your way down. The cost you get at (N,N) will be the minimal cost.
Here is my solution with dynamic - programming in O(n^2)
you start with (1,1) so you can find say a = (1,2) and b = (2,1) by a = value(1,1) + value(1,2). Then, to find (2,2) select the minimum (a+ value(2,2)) and (b + value(2,2)) and continue with this logic. You can find any minimum sum among (1,1) and (i,j) with that algorithm. Let me explain,
Given Matrix
1 2 3
4 5 6
7 8 9
Shortest path :
1 3 .
5 . .
. . .
so to find (2,2) take the original value(2,2)=5 from Given Matrix and select min(5 + 5), 3 + 5) = 8. so
Shortest path :
1 3 6
5 8 .
12 . .
so to find (3,2) select min (12 + 8, 8 + 8) = 16 and (2,3) = min(8 + 6, 6 + 6) = 12
Shortest path :
1 3 6
5 8 12
12 16 .
so the last one (3,3) = min (12 + 9, 16 + 9) = 21
Shortest path :
from (1,1) to any point (i, j)
1 3 6
5 8 12
12 16 21
You can convert the grid into a graph. The edges get the weights of the values from your grid elements. Then you can find the solution with the shortest path problem.
start--1--+--2--+--3--+
| | |
4 5 6
| | |
+--5--+--6--+
| | |
7 8 9
| | |
+--8--+--9--end
Can someone explain how to approach the problem?
Read about dynamic programming and go from there.
Attempt:
Start with the first row and calculate the cumulative values and store them.
Proceed to the second row, now the values could have only come from the left or top (since you can only go left or down), calculate the smallest of the cumulative values for this row.
Iterate down the rows until the last and you'll be able to get the smallest value when you reach the last node.
I claim this algorithm is O(n) since if you use a 2 dimensional array you only need to access all fields at most twice (read from top, left) for read and once for write.
If you want to go really fancy or have to operate on massive matrices, A* could also be an option.

Resources