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