본문 바로가기
HackerRank Algorithm

[HackerRank] 22. Between Two Sets

by KIha_Jung 2020. 4. 7.

Between Two Sets

  • You will be given two arrays of integers and asked to determine all 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
  • 문제 요약

    • A의 요소로 나누어 떨어지고 B 요소를 나누어 떨어지게 하는 수의 개수

# 방법1

import math
from functools import reduce
#
# 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

# Greate Common Divisor(최대공약수)
# 참고. 유클리드 호제법의 공식
# 최대공약수를 구하는 함수를 gcd(x, y)라고 가정
# x % y = 0 이라면 gcd(x, y) = y가 성립
# x % y != 0 이라면 gcd(x, y) = gcd(x, x % y)가 성립

# Least Common Multiple(최소공배수)
# 참고. 유클리드 호제법 공식
def getTotalX(a, b):
    total = 0
    for num in range(max(a),min(b)+1):
        for numA in a:
            if num%numA != 0:
                break
        else:
            for numB in b:
                if numB%num != 0:
                    break
            else:
                total += 1
    return total

if __name__ == '__main__':
    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)
    print(total)

'HackerRank Algorithm' 카테고리의 다른 글

[HackerRank] 24. Electronics shop  (0) 2020.04.07
[HackerRank] 23. Apple and orange  (0) 2020.04.07
[HackerRank] 21. Birthday cake candles  (0) 2020.04.01
[HackerRank] 20. Birthday chocolate  (0) 2020.04.01
[HackerRank] 19. Bon Appétit  (0) 2020.04.01

댓글