Note that there are some explanatory texts on larger screens.

plurals
  1. POalgorithm to compare a sorted array
    primarykey
    data
    text
    <p>I have two sorted arrays. I need to find if both are different r not.</p> <p>These arrays have elements in a <strong>specific range</strong>.</p> <p><strong>More that one</strong> element may be different.</p> <p>Arrays can have different sizes. In this case I should be able to point out the differences</p> <p><strong>A rough example:</strong></p> <p>Input: </p> <pre><code>array1: 1 2 4 5 8 9 12 array2: 1 4 8 10 12 13 14 </code></pre> <p>Here the range is 1-15.</p> <p>What is the most optimum compare algorithm?</p> <p>I should be able to point out the differences and similarities too, e.g. 4 is in both and 5 is missing in the second array.</p> <p><strong>My solution:</strong></p> <ol> <li><p>Two pointer to keep track of the index of the array.</p></li> <li><p>Point them to the start of the array.</p></li> <li><p>Start compare the first two elements.</p></li> <li><p>If both are equal--> move to the next one.<br> else </p> <ol> <li><p>Find the largest of the two elements of the array. say array1 has the larger element.</p></li> <li><p>Binary search for the element in the other array.(array2) --> pos of that element in that array say pos</p></li> <li><p><strong>Discard</strong> the elements of the array till pos.</p></li> </ol></li> <li><p>Increment pointers. <strong>discard</strong> that part of array till this pointers. repeat.</p></li> </ol> <p>This has a complexity of <code>n log n</code> (much less than that on average, this is when you have to do a search for every element).</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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