(Maximize LCM) Doubt in Solution.. Why it is showing wrong answer?

#include<bits/stdc++.h>
#include<stdio.h>
typedef long long int ll;
#define pb push_back
#define arr vector
#define mem(x) memset(x,0,sizeof(x))
#define vp(x) vector<pair<ll,ll>>x
#define matrix vector<vector >
using namespace std;
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
ll findlcm(ll a[], ll n)
{
ll ans = a[0];

 for (ll i = 1; i < n; i++) 
    ans = (((a[i] * ans)) / 
            (gcd(a[i], ans))); 

return ans; 

}
int main(){
ll t;
cin>>t;
while (t–){
ll n,m;
cin>>n>>m;
ll a[100000];
for (ll i=0;i<n;i++){
cin>>a[i];
}
ll ans=0,ansx,maxx=INT_MIN;
for (ll i=m;i>=1;i–){
a[n]=i;
ansx=findlcm(a,n+1);
maxx=max(maxx,ansx);
if (ansx==maxx){
ans=i;
}
}
cout<<ans<<endl;
}
return 0;
}

@hardikvyas11
In this case , LCM may get out of the range of long long data type for some test case.
for example if there is an array which have only prime numbers till the given range 10^4, then see your lcm wil be get outofbound.

1 Like

@akman10
Oh! Got it ! Thank you :slight_smile:

1 Like