Since your question only makes sense if the arrays are ordered, you can find the intersection point with the binary search, which will be much faster for large arrays:
# Find first index of a2 > a1 def crossover(a1, a2): bottom = 0 top = min(len(a1) - 1, len(a2) - 1) if a2[top] <= a1[top]: return -1 while top > bottom: mid = (top + bottom) >> 1 if a2[mid] > a1[mid]: top = mid else: bottom = mid + 1 return top f1 = [ (x + 20) for x in range(80) ] f2 = [ (2 * x) for x in range(100) ] print( crossover( f1, f2 ) )
This should (and does) type "21".
source share