I'm doing a coding challenge and stumbled upon this question: There will be two arrays of integers. Determine the number of integers that satisfy the following two conditions:
- The elements of the first array are all factors of the integer being considered
- The integer being considered is a factor of all elements of the second array
These numbers are referred to as being between the two arrays. Determine how many such numbers exist.
Example:
- a=[2,6]
- b=[24,36]
There are two numbers between the arrays: 6 and 12
- 6%2=6%6=12%2=12%6=0
- 24%6=36%6=24%12=36%12=0
This is what I did on the function getTotalX:
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'getTotalX' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY a
# 2. INTEGER_ARRAY b
#
def getTotalX(a, b,k,count):
flag = 0
p=count
l = max(b)
if(k<=l):
for i in range(len(a)):
if(k%a[i]!=0):
flag=1
for j in range(len(b)):
if(b[j]%k!=0):
flag=1
if(flag==0):
count+=1
getTotalX(a,b,k+1,count)
else:
getTotalX(a,b,k+1,count)
else:
return p
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
m = int(first_multiple_input[1])
arr = list(map(int, input().rstrip().split()))
brr = list(map(int, input().rstrip().split()))
total = getTotalX(arr, brr,max(arr),0)
fptr.write(str(total) + '\n')
fptr.close()