본문 바로가기
1 Day 1 Algorithms

[2019.01.31] Between Two Sets

by 곰돌찌 2019. 1. 31.

Problem


You will be given two arrays of integers and asked to determine all 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. You must determine how many such numbers exist.

For example, given the arrays  and , there are two numbers between them: and . , ,  and  for the first value. Similarly, ,  and , .

Function Description

Complete the getTotalX function in the editor below. It should return the number of integers that are betwen the sets.

getTotalX has the following parameter(s):

  • a: an array of integers
  • b: an array of integers

Input Format

The first line contains two space-separated integers, and , the number of elements in array  and the number of elements in array 
The second line contains  distinct space-separated integers describing  where 
The third line contains  distinct space-separated integers describing  where .

Constraints

Output Format

Print the number of integers that are considered to be between  and .

Sample Input

2 3
2 4
16 32 96

Sample Output

3

Explanation

2 and 4 divide evenly into 4, 8, 12 and 16. 
4, 8 and 16 divide evenly into 16, 32, 96.

4, 8 and 16 are the only three numbers for which each element of a is a factor and each is a factor of all elements of b.


How I solved the problem


# 해결 방법 (Discussion 참고했음!)

  - 첫 번째 array들의 최소 공배수를 구함.

  - 두 번째 array들의 최대 공약수를 구함

  - 최소 공배수의 배수 ( 최소 공배수 * 1, 최소 공배수 * 2...)를 해서 최대 공약수를 나누었을 때 나머지가 0인 인수들을 체크


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
 
public class Solution {
 
    /*
     * Complete the getTotalX function below.
     */
    static int getTotalX(int[] a, int[] b) {
        /*
         * Write your code here.
         */
        int gcdInt = gcd(b); //최대 공약수
        int lcmInt = lcm(a); //최소 공배수
 
        int count = 0;
        int index = 1;
        int comp = 0;
 
        while (comp < gcdInt) {
            comp = lcmInt * index;
 
            if (gcdInt % comp == 0) {
                count ++;
            }
 
            index++;
        }
 
        return count;
    }
 
    private static int gcd(int a, int b) {
        while (b > 0){
            int temp = b;
            b = a % b; // % is remainder
            a = temp;
        }
 
        return a;
    }
 
    private static int gcd(int[] input){
        int result = input[0];
 
        for(int i = 1; i < input.length; i++) {
            result = gcd(result, input[i]);
        }
 
        return result;
    }
 
    private static int lcm(int a, int b){
        return a * (b / gcd(a, b));
    }
 
    private static int lcm(int[] input){
        int result = input[0];
 
        for(int i = 1; i < input.length; i++) {
            result = lcm(result, input[i]);
        }
 
        return result;
    }
 
    private static final Scanner scan = new Scanner(System.in);
 
    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
 
        String[] nm = scan.nextLine().split(" ");
 
        int n = Integer.parseInt(nm[0].trim());
 
        int m = Integer.parseInt(nm[1].trim());
 
        int[] a = new int[n];
 
        String[] aItems = scan.nextLine().split(" ");
 
        for (int aItr = 0; aItr < n; aItr++) {
            int aItem = Integer.parseInt(aItems[aItr].trim());
            a[aItr] = aItem;
        }
 
        int[] b = new int[m];
 
        String[] bItems = scan.nextLine().split(" ");
 
        for (int bItr = 0; bItr < m; bItr++) {
            int bItem = Integer.parseInt(bItems[bItr].trim());
            b[bItr] = bItem;
        }
 
        int total = getTotalX(a, b);
 
        bw.write(String.valueOf(total));
        bw.newLine();
 
        bw.close();
    }
}
 
cs


[출처 : https://www.hackerrank.com ]


#최소 공배수 최대 공약수 구하기 출처

https://stackoverflow.com/questions/4201860/how-to-find-gcd-lcm-on-a-set-of-numbers


'1 Day 1 Algorithms' 카테고리의 다른 글

[2019.02.08] Birthday Chocolate  (0) 2019.02.08
[2019.02.01] Breaking the Records  (0) 2019.02.01
[2019.01.30] Kangaroo  (0) 2019.01.30
[2019.01.29] Apple and Orange  (0) 2019.01.29
[2019.01.28] Grading Students  (0) 2019.01.28

댓글