Is there an algorithm that will take two columns of numbers and determine if they have a value in common?
Two columns:
One increases by .3 with each value. The other increases erratically. I want to determine if the two columns have a value in common.
.3 .1
.6 .2
.9 .4
1.2 .5
1.5 .9
1.8 .13
If both series are increasing (or sorted1 in general), there is an
O(n)solution. You can’t do any better than that, because you need to look at every element in the erratically-increasing list at least once in the worst case.Simply traverse both at the same time in a zipper-like fashion: Always get the next element from the list, that currently has a smaller element than the other one.
1Where sorted means with respect to a total order.