MAXIMIZE LCM (MXMLCM)

CAN ANYONE TELL ME WHAT IS WRONG WITH MY CODE OR LOGIC??
it is showing wrong answer

#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;

ll findlcm(int,int,int);

int gcd(int,int);

int gcd(int a,int b)

{

if(a==0)

    return b;

return gcd(b%a,a);    

}

ll findlcm(int arr[] ,int n,int m)

{

ll ans=arr[0];

for(int i=1;i<n;i++)

ans= (((arr[i]*ans))/(gcd(arr[i],ans)));

ll a;

int res=0;

for(int j=1;j<=m;j++)

{

    a= (((j*ans))/(gcd(j,ans)));

    if(a>ans)

        res=j;

    else if(a==ans && res==0)

        res=j;

} 

return res;  

}

int main()

{

int t;

cin>>t;

if(t< 1 || t>100)

    exit(0);

while(t!=0)

{

    int n,m;

    cin>>n>>m;

    int arr[n];

    for(int i=0;i<n;i++)

    {

        cin>>arr[i];

    }

    printf("%d\n" , findlcm(arr,n,m));

    t--;

} 

return 0;   

}

same problem with me

The LCM can be very large that even long long int cannot hold. so this will give you WA, to get correct answer you can use sieve. Find LCM of given N integers in form of its prime factors.

1 Like

Numbers can be very large whose product can’t fit even in long long int.

1 Like

okay thank you so much