I have to design an algorithm that compares two sorted lists of the same length and return the number of common values between them.
So if I have two lists a = [2, 9, 15, 27, 36, 40] and b = [9, 11, 15, 23, 36, 44], the algorithm should return the value of 3 as 9, 15 and 36 are present in both lists.
I know that there might be an easier to do with using sets, but since I'm trying to learn data structures and algorithms I'd prefer to do it the longer(harder way).
My current code uses any array merge algorithm which is non-working at the moment as I'm still confused as to r1 and r2, though i think they would be the right most element in the array, but I don't know how to get that. eg. r1 = 40 (from list a), and r2 = 44 (from list b)?
global a
a = [2, 9, 15, 27, 36, 40]
global b
b = [9, 11, 15, 23, 36, 44]
global c
c = []
def merge (a1, a, r1, a2, b, r2, c, list3):
i = a
j = b
k = c
r1 =
r2 =
while i <= r1 and j <= r2:
if a1[i]<=a2[j]:
a3[k] = a1[i]
i += 1
elif a3[k] >= a2[j]:
j += 1
k += 1
while i <= r1:
a3[k] = a1[i]
i += 1
k += 1
while j <= r2:
a3[k] = a2[j]
j += 1
k += 1
Thank you for the help and feedback.