Finding a pair from a 2D vector i - vector

Finding a pair from a 2D vector if it's second element is not present in the first element of another pair
Finding a pair from a 2D vector if it's second element is not present in the first element of another pair

Please add more clarity to problem.
As I got:
create a map for first elements indicating if first element is present or not by iterating once.
In second iteration look for second element and search if it is there in map as or not.
If not present then return it, else move to next one.

Related

Graph - Algorithm to position nodes

I am trying to create a dynamic graph where users can add new nodes using ELK.js
The graph is a tree that has one root node. I am trying to set the position of nodes in a row using (x,y) position (y is not important for now).
Assumptions:
Lower x value brings the node to the left.
Two nodes in one row can't have the same x value.
When we add a new node it should appear on the right of other children if available (the green box in row 2 and 3 for example are a new nodes)
New nodes can be added to every row at any moment (green box in row 2 and row 3)
Max number we can use to set x value is 16 digit long: 9999999999999999
A simple example of how positions behave can be found here (see the position of nodes n2, n3, n4 and change them in JSON)
I am trying not to calculate every position of every node in a row. I tried a lot of different numbers but I stuck and need fresh ideas.
Any help would be appreciated. Thank you
You could approach this as follows:
When a new node is the first one on its level, give it 0 as its x-value.
When it is not the first, find out what its immediate two siblings are on that level (one at the left, one at the right of the node). In some cases you'll need to traverse from the node via one or more of its ancestor(s) to find such immediate sibling.
Get the x-values of these two siblings, and take the average of those two values for the new node's x value.
It might be that there is only a sibling at one side. If there is no sibling at the right side, take the average between the left siblings's x-value and 1016. If it is the left sibling that is missing, take the average between the right siblings's x-value and -1016.
This practically means you use an initial range of -1016...1016 and keep cutting segments in half when a new node must be placed within a segment.

How to find a polygon which a single point lies on

I have a list of polygons and a single point with its latitude and longitude. I want to find a polygon which the single point lies on.
Currently, I'm iterating over the whole list to find the answer, which seems a bit inefficient. Is there a better way to make it?
Not unless you have some prior information about this list. If you are given a list with no prior knowledge of its elements, you must check every item in the list to see if it meets the condition.

Finding nearest element to a point STL format

A short introduction:
I have a body that is expressed by the outer-surface, given in STL form (set of triangular elements and their outside-pointing normal vector).
I'm trying to detect if a point given by coordinates is inside or outside the body.
The problem:
How do I find the nearest element to a given point?
More specifically, say you have found the nearest vertex to the point and this vertex is shared by two (or more) elements. Which is is the "nearest" one?
Note that the end result is determining if the point is inside or outside the body. A simple normal distance (dot product with the normal) does not solve the problem and can lead to ambiguous result based on which of the elements, sharing the node is selected.
Using the centroid of the element is also problematic.
Any suggestions, ideas (especially from people who have been involved in this issue before) are most welcomed!
EDIT:
I'll make the issue slightly harder. Say there's an open surface (but it covers the whole domain so that every point is on one of two sides of the surface, either in or out, based on the direction relative to the normal.
This also needs to be answered using the same approach.
EDIT2:
Answer was found!
Hope this helps!
The problem was answered with a variation of the ray-intersection method. 1. Find the nearest vertex, using the L2 norm (squared). 2. Consider the elements which share the vertex. It is recommended to have a connectivity list and not map them every time. 3. Cast a ray is cast from the interest point to the centroid of the first element. 4. Check among the elements in <2> which intersect the ray and select the one with the shortest intersection distance 5. Use the dot product of the intersection vector with the element normal (negative sign = outside, else inside)
(This was added to the question post itself)

Making elements of a matrix multiples of 42 without changing sum of any row or column

I stumbled across this interesting question: https://prologin.org/train/2016/semifinal/42_le_retour (in French).
In short, we're given a matrix composed of random numbers, i.e.:
40 0 2
0 84 0
2 42 40
The objective is to modify the elements in this matrix so that they are all divisible by 42; however, the sum of any row and column should remain the same as before any modification. (The original problem was to calculate the minimum clicks needed, supposing each click adds or subtracts 1)
I am wondering whether and why this is always possible.
Possible moves in a 3x3 matrix are:
adding +-1 to a corner value
adding +-1 to the opposing corner
adding -+1 to the neighboring corner
2.
adding +-1 to a corner value
adding -+1 to the neighbor values
adding +-1 to the middle value
The first possibility has two variants, differing only in sign. The second possibility has a total of 8 variants (four different corners and different sizes).
I do not know whether this is always possible, especially since we do not know the limits of the matrix and it is unclear whether we have a square matrix at all. However, you can check this using backtracking. Whenever you reach a matrix where each element modulo 42 are the same as initially, you need to trace back, until you find a solution.

Right to Left Match Count Non-Blank Cells

How do I modify this formula:
=MATCH(1,--(AA1:AH1=""),0)-1
to count from right to left? I want to count the number of non-blank cells until a blank cell, but I need to do it starting from right to left.
The array formula (confirmed with ctrl+shift+enter)
=MAX(IF(LEN(AA1:AH1)<>0,COLUMN(AA1:AH1)))
should give you the index of the column containing the last non-blank cell, even in a row with multiple intercalated blank cells.
The array formula (confirmed with ctrl+shift+enter)
=MAX(IF(LEN(AA1:AH1)=0,COLUMN(AA1:AH1)))
should give you the index of the column containing the last blank cell, even in a row with multiple intercalated blank cells.
You could then use this values to calculate your desired results.
i.e.:
To put it all together:
=MAX(IF(LEN(AA1:AH1)<>0,COLUMN(AA1:AH1))) - MAX(IF(LEN(AA1:AH1)=0,COLUMN(AA1:AH1)))
should give you your desired result.
I don't know how to do it with match() function, but you can do it otherwise with following with Ctrl+Shift+Enter:
=8-MAX(IF(AA1:AG1="",COLUMN(AA1:AG1)-26))
Thank You,

Resources