0

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:

  1. The elements of the first array are all factors of the integer being considered
  2. 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:

  1. a=[2,6]
  2. b=[24,36]

There are two numbers between the arrays: 6 and 12

  1. 6%2=6%6=12%2=12%6=0
  2. 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()

This is the error message I'm getting

  • post the error message – Beatdown Jun 24 '22 at 11:56
  • please add it to the post itself, your post is not complete without this – Beatdown Jun 24 '22 at 11:59
  • Did you see the edit?Please reply – Konduru Premsai Jun 24 '22 at 12:06
  • What is your question? The error message tells you that your code is not fast enough and your code doesn't print anything to stdout by the look of it – Iain Shelvington Jun 24 '22 at 12:10
  • @KonduruPremsai for optimization, consider adding `break` statements within your code. However that is not the main problem here. What is making your code timeout is the recursion. What is the recursion meant to accomplish? You are not even catching the output. – Allie Jun 24 '22 at 12:13
  • The aim of recursion is to check for the possibilities between k(i.e., max(a)) and l(i.e., max(b)) as we have to find the number of possible numbers to get divided off by elements in a and divide elements in the array b – Konduru Premsai Jun 24 '22 at 13:24
  • @Allie I used flag instead of break – Konduru Premsai Jun 24 '22 at 13:25
  • in recurision inside `def getTotalX(...)` you have use `return` - like `return getTotalX(...)` - to send result back to previous execution. OR you have to use `result = getTotalX(...)` and later `return result` – furas Jun 24 '22 at 13:40
  • if it needs response on stdout then use `print()` or `sys.stdout.write()` – furas Jun 24 '22 at 13:41
  • Do you want all the integers, or the _number_ of integers that satisfy the question? Your question says that you want both, but the code says to return a single integer. – BrokenBenchmark Jun 25 '22 at 02:29
  • @BrokenBenchmark! No The question says to return number of integers that satisfy. Sorry for not posting the complete question. I too have seen that just now – Konduru Premsai Jun 26 '22 at 03:24
  • Your question says to "Determine all integers that satisfy the following two conditions" -- it would be good to amend the wording using an [edit]. – BrokenBenchmark Jun 26 '22 at 03:26
  • If my response answers your question, please accept it as the answer. If not, leave a comment or edit your post to provide more detail. Thanks! – Caleb Keller Aug 12 '22 at 23:24

1 Answers1

1

Try this:

import os
from functools import reduce

# https://stackoverflow.com/questions/6800193/what-is-the-most-efficient-way-of-finding-all-the-factors-of-a-number-in-python
def factors(n):    
    return set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))

def intersectAll(a, b):
    if a is set:
        return a.intersection(factors(b))
    else:
        return factors(a).intersection(factors(b))

def getTotalX(a, b):
    minVal = max(a)
    bfactors = filter(lambda x: x >= minVal, reduce(intersectAll, b))
    
    result = []
    
    for factor in bfactors:
        success = True
        for number in a:
            if factor % number != 0:
                success = False
                break
        if success:
            result.append(factor)
    
    return len(result)

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)

    fptr.write(str(total) + '\n')

    fptr.close()

It works on your test case. Probably not the most efficient, but it should be fast enough on reasonably-sized inputs.

Caleb Keller
  • 553
  • 3
  • 19
  • can't understand why you kept this here.Please tell me wat I should watchout in this – Konduru Premsai Jun 24 '22 at 14:41
  • I'm not sure what you mean - if you include these functions in your code then pass the arrays to the getTotalX function like you did in your original code, the number it returns is the answer. I didn't include any of the input or output code here because you already have it in your `if __name__ == "main"` block. – Caleb Keller Jun 24 '22 at 14:49
  • I edited the answer so you can just copy and paste the whole thing. – Caleb Keller Jun 24 '22 at 15:00