Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If</p> <ul> <li>by "modification is not allowed" it was meant "you may change but in the end they should be restored", and</li> <li>we could iterate the lists exactly <em>twice</em></li> </ul> <p>the following algorithm would be the solution.</p> <p>First, the numbers. Assume the first list is of length <code>a+c</code> and the second one is of length <code>b+c</code>, where <code>c</code> is the length of their common "tail" (after the mergepoint). Let's denote them as follows:</p> <pre><code>x = a+c y = b+c </code></pre> <p>Since we don't know the length, we will calculate <code>x</code> and <code>y</code> without additional iterations; you'll see how.</p> <p>Then, we iterate each list and reverse them while iterating! If both iterators reach the merge point at the same time, then we find it out by mere comparing. Otherwise, one pointer will reach the merge point before the other one.</p> <p>After that, when the other iterator reaches the merge point, it won't proceed to the common tail. Instead will go back to the former beginning of the list that had reached merge-point before! So, before it reaches the end of the changed list (i.e. the former beginning of the other list), he will make <code>a+b+1</code> iterations total. Let's call it <code>z+1</code>.</p> <p>The pointer that reached the merge-point first, will keep iterating, until reaches the end of the list. The number of iterations it made should be calculated and is equal to <code>x</code>.</p> <p>Then, this pointer iterates back and reverses the lists again. But now it won't go back to the beginning of the list it originally started from! Instead, it will go to the beginning of the other list! The number of iterations it made should be calculated and equal to <code>y</code>.</p> <p>So we know the following numbers:</p> <pre><code>x = a+c y = b+c z = a+b </code></pre> <p>From which we determine that</p> <pre><code>a = (+x-y+z)/2 b = (-x+y+z)/2 c = (+x+y-z)/2 </code></pre> <p>Which solves the problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload