You are given a list of distances between various points on a single line.
For example:
- 100 between a and b
- 20 between c and b
- 90 between c and d
- 170 between a and d
Return the sorted sequence of points as they appear on the line with distances between them:
For example the above input yields:
a<—-80—–> c <—-20——> b <—-70—–> d or the reverse sequence(doesn’t matter)
What is this problem called? I would like to research it.
If anybody knows, also, what are some of the possible asymptotic runtimes achieved for this?
not sure it has a name; more formally stated, it would be:
where
|x|stands for the absolute value of xAs far as the generalized system goes, if you have N equations of this type with k unknowns, you have N choices of sign. Without loss of generality (because any solution yields a second solution with reversed ordering) you can choose a sign for the first equation, and a particular value for one of the unknowns (since the whole thing can slide left and right in position). Then you have 2N-1 possibilities for the remaining equations, and all you have to do is go through them to see which ones, if any, have solutions. Because the coefficients are all +/- 1 and each equation has 2 unknowns, you just go through them one by one:
So an upper bound for the runtime is O(N * 2N-1) ≡ O(N * 2N).
I suppose there could be a shortcut but no obvious one comes to mind.