Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I can think of O(N*K*N)(edited after correctly pointed out by zivo, not a good solution now :( ) solution.<br> 1. Take N pointer initially pointing to initial element each of N arrays.</p> <pre><code>6, 16, 67 ^ 11,17,68 ^ 10, 15, 100 ^ </code></pre> <p>2. Find out the highest and lowest element among the current pointer O(k) (6 and 11) and find the difference between them.(5)<br> 3. Increment the pointer which is pointing to lowest element by 1 in that array.</p> <pre><code> 6, 16, 67 ^ 11,17,68 ^ 10, 15, 100 (difference:5) ^ </code></pre> <p>4. Keep repeating step 2 and 3 and store the minimum difference. </p> <pre><code> 6, 16, 67 ^ 11,17,68 ^ 10,15,100 (difference:5) ^ 6, 16, 67 ^ 11,17,68 ^ 10,15,100 (difference:2) ^ </code></pre> <p><strong>Above will be the required solution.</strong> </p> <pre><code> 6, 16, 67 ^ 11,17,68 ^ 10,15,100 (difference:84) ^ 6, 16, 67 ^ 11,17,68 ^ 10,15,100 (difference:83) ^ </code></pre> <p>And so on......</p> <h2>EDIT:</h2> <p>Its complexity can be reduced by using a heap (as suggested by Uri). I thought of it but faced a problem: Each time an element is extracted from heap, its array number has to be found out in order to increment the corresponding pointer for that array. <strong>An efficient way to find array number can definitely reduce the complexity to O(K*N log(K*N))</strong>. One naive way is to use a data structure like this</p> <pre><code>Struct { int element; int arraynumer; } </code></pre> <p>and reconstruct the initial data like</p> <pre><code> 6|0,16|0,67|0 11|1,17|1,68|1 10|2,15|2,100|2 </code></pre> <p>Initially keep the current max for first column and insert the pointed elements in heap. Now each time an element is extracted, its array number can be found out, pointer in that array is incremented , the newly pointed element can be compared to current max and max pointer can be adjusted accordingly.</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