-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSherlockAndGCD.java
More file actions
42 lines (32 loc) · 804 Bytes
/
SherlockAndGCD.java
File metadata and controls
42 lines (32 loc) · 804 Bytes
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
//HackerRank Sherlock and GCD
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int tc = 0; tc < T; tc++) {
int N = sc.nextInt();
int[] a = new int[N];
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
System.out.println(solve(a) ? "YES" : "NO");
}
sc.close();
}
static boolean solve(int[] a) {
int g = a[0];
for (int i = 1; i < a.length; i++) {
g = gcd(g, a[i]);
}
return g == 1;
}
static int gcd(int x, int y) {
return (y == 0) ? x : gcd(y, x % y);
}
}