i don’t know whats the mistake in my code i have tested more than 100 cases manually and all of them giving correct answer please help!!!
Here is my code–
using namespace std;
#define ll long long int
ll gcd(ll a, ll b)
if (b == 0)
return gcd(b, a % b);
ll findlcm(ll arr, ll n)
ll ans = arr;
for (ll i = 1; i < n; i++)
ans = (((arr[i] * ans)) /
// Function to return LCM of two numbers
ll lcm(ll a, ll b)
return (a*b)/gcd(a, b);
ll ans = findlcm(arr,n);
ll val = lcm(ans,i);
return 0 ;
When you use lcm, you multiply all the numbers which causes overflow.
Even if you are using long long int, it is overflowing.
So, This is why you are getting WA.
P.S:- Check out about cpp_int and other int types of boost (however this will also give you partially correct answer because finding gcd is of logn).
I tried it using unsigned long long int (largest data type) but still it’s giving me WA is it still be the case of interger overflow? or there is some logic error
You can store the lcm value into power of primes and try.
No Even if you use unsigned long long int, it wil still overflow because you are multiplying too many numbers … even if you multiply 7 & 11, It gives 77 as result now you can think.
P.S:- Check Out cpp_int and other int data type of boost (however it will give you PA because finding gcd takes logn).
Can you please elaborate the logic?
Your soluion will result in an overflow. Try using factorization as every number can be expressed as the product of primes. Look at this once. I think it will be clear.