-
Notifications
You must be signed in to change notification settings - Fork 5
/
DivideArrayByGCD.java
69 lines (59 loc) · 1.27 KB
/
DivideArrayByGCD.java
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
/* Take input an array, then divide the whole array by there gcd.
* Input-1
* ----------
* 5
* 36 12 9 48 15
*
* Output-1
* ----------
* 12 4 3 16 5
*
* Input-2
* ----------
* 5
* 31 51 47 63 24
*
* Output-2
* ----------
* 31 51 47 63 24
*/
import java.util.*;
public class DivideArrayByGCD {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
// Take array
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// Find minimum number
int min = arr[0];
for (int i = 0; i < arr.length; i++) {
if(arr[i]<min)
{
min = arr[i];
}
}
// Find GCD
int gcd = 1;
for (int i = 1; i <= min; i++) {
boolean f = true;
for (int j = 0; j < arr.length; j++) {
if(arr[j] % i != 0)
{
f = false;
}
}
if(f == true)
{
gcd = i;
}
}
// Find Solution
for (int i = 0; i < arr.length; i++) {
int sol = arr[i]/gcd;
System.out.print(sol+" ");
}
}
}