Skip to content

Commit

Permalink
math
Browse files Browse the repository at this point in the history
  • Loading branch information
LLLgoyour committed Oct 19, 2023
1 parent 8fbae9b commit c13f552
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions CSES/Mathematics/Common Divisors/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 1,43 @@
import java.io.*;
import java.util.*;

public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] line = br.readLine().split(" ");
br.close();

int[] numbers = new int[n];
for (int i = 0; i < n; i ) {
numbers[i] = Integer.parseInt(line[i]);
}

int maxGCD = findMaxGCD(numbers, n);
System.out.println(maxGCD);
}

public static int findMaxGCD(int[] numbers, int n) {
int maxNumber = Arrays.stream(numbers).max().getAsInt();
int[] count = new int[maxNumber 1];

for (int num : numbers) {
for (int i = 1; i * i <= num; i ) {
if (num % i == 0) {
count[i] ;
if (i != num / i) {
count[num / i] ;
}
}
}
}

for (int i = maxNumber; i >= 1; i--) {
if (count[i] > 1) {
return i;
}
}

return 1;
}
}

0 comments on commit c13f552

Please sign in to comment.