# Robinhood OA

``````def maxArithmeticLength(a, b):

ap = set()
max_count = -1

# checking which difference have to be checked for AP
if(len(a)==1):
else:
diff = a[1]-a[0] # TLE
for i in range(1, diff+1):

for ap_val in ap:
a_pointer = 1
b_pointer = 0
current_value = a[0]

while a_pointer<len(a) and b_pointer<len(b):
if a[a_pointer] - current_value == ap_val:
current_value = a[a_pointer]
a_pointer = a_pointer + 1
elif b[b_pointer] - current_value == ap_val:
current_value = b[b_pointer]
b_pointer = b_pointer + 1
else:
b_pointer = b_pointer + 1

# if elements of a are left
while a_pointer<len(a):
if a[a_pointer] - current_value == ap_val:
current_value = a[a_pointer]
a_pointer = a_pointer + 1
else:
break

# if elements of b are left
while b_pointer<len(b):
if b[b_pointer] - current_value == ap_val:
current_value = b[b_pointer]
b_pointer = b_pointer + 1

# add_rev_element - my first element is always A[0], so any number less than that to be searched in B
pointer = a[0]
while pointer - ap_val > 0:
neg_sum = pointer - ap_val
if(neg_sum in b):
pointer = neg_sum
else:
break

if a_pointer<len(a):
max_count = max(max_count, -1)
else:

return(max_count)

if __name__ == "__main__":
a = [0, 4, 8, 16]
b = [0, 2, 6, 12, 14, 20]
print(maxArithmeticLength(a, b) == 6)

a = [5, 7, 13, 14]
b = [9, 11, 15]
print(maxArithmeticLength(a, b) == -1)

a = [20, 22]
b = [19, 21, 23, 24, 26, 28]
print(maxArithmeticLength(a, b) == 6)

a = [179, 335, 647, 699, 959, 1011, 1635, 2051, 2103, 2415, 2623]
b = [2, 12, 24, 27, 45, 53, 55, 74, 82, 87, 95, 111, 117, 119, 120, 123, 127, 128, 129, 138, 160, 168, 176, 198, 199,
224, 229, 231, 263, 283, 300, 308, 319, 322, 340, 341, 353, 387, 411, 415, 429, 438, 439, 443, 446, 466, 468,
491, 518, 525, 531, 539, 543, 569, 570, 587, 594, 595, 751, 803, 855, 907, 1063, 1115, 1167, 1219, 1271, 1323,
1375, 1427, 1479, 1531, 1583, 1687, 1739, 1791, 1843, 1895, 1947, 1999, 2155, 2207, 2259, 2311, 2363, 2467,
2519, 2571]
print(maxArithmeticLength(a, b) == 49)

a = [7, 13]
b = [1, 10, 16]
print(maxArithmeticLength(a, b) == 4)

a = [20, 22]
b = [19, 21, 23, 24, 26, 28]
print(maxArithmeticLength(a, b) == 6)
``````
1 Like