using
System;
using
System.Collections.Generic;
class
GFG{
static
List<
int
> []primeFactors =
new
List<
int
>[10001];
static
HashSet<
int
> s =
new
HashSet<
int
>();
static
void
findPrimeFactors()
{
bool
[]primes =
new
bool
[10001];
for
(
int
i = 0; i < 10001; i++)
primes[i] =
true
;
for
(
int
i = 2; i < 10001; i++) {
if
(primes[i]) {
for
(
int
j = i; j < 10001; j += i) {
if
(j != i) {
primes[j] =
false
;
}
primeFactors[j].Add(i);
}
}
}
}
static
void
primeFactorsofLCM(
int
[]frequecyOfPrimes,
int
[] arr,
int
n)
{
for
(
int
i = 0; i < n; i++) {
foreach
(
int
a
in
primeFactors[arr[i]]) {
int
k = 0;
while
((arr[i] % a) == 0) {
arr[i] /= a;
k++;
}
frequecyOfPrimes[a]
= Math.Max(frequecyOfPrimes[a], k);
}
}
}
static
int
elementToBeAdded(
int
[]frequecyOfPrimes,
int
m)
{
int
product = 1;
int
ans = 1;
for
(
int
i = 2; i <= m; i++) {
if
(s.Contains(i))
continue
;
int
j = i;
int
current = 1;
foreach
(
int
a
in
primeFactors[j]) {
int
k = 0;
while
((j % a) == 0) {
j /= a;
k++;
if
(k > frequecyOfPrimes[a]) {
current *= a;
}
}
}
if
(current > product) {
product = current;
ans = i;
}
}
return
ans;
}
static
void
findElement(
int
[] arr,
int
n,
int
m)
{
for
(
int
i = 0; i < n; i++)
s.Add(arr[i]);
int
[]frequencyOfPrimes =
new
int
[10001];
primeFactorsofLCM(frequencyOfPrimes, arr, n);
Console.Write(elementToBeAdded(frequencyOfPrimes, m)
+
"\n"
);
}
public
static
void
Main(String[] args)
{
for
(
int
i = 0; i < 10001; i++)
primeFactors[i] =
new
List<
int
>();
findPrimeFactors();
int
N = 5;
int
M = 9;
int
[]arr = { 2, 5, 3, 8, 1 };
findElement(arr, N, M);
}
}