Why do i get wrong ans even if it gives a desired output?

my solution link :Solution: 45084340 | CodeChef
please help

When you are finding lcm that time you are multiplying (a*b) which is resulting in integer overflow .You should use long long int instead of int to avoid the overflow.
See here i have changed int to long long int in your solution

2 Likes

Use long long int instead of int even for a and b. This is because (a*b) is stored in int data type and then divided by GCD which causes the integer overflow! in order to understand by yourself, please the test case :slight_smile:
5
120 140
10213 312
10 30
1000000 1000000
1000000 999999
The output should be :
20 840
1 3186456
10 30
1000000 1000000
1 999999000000
You just have to change int to long long int in the gcd function as well!! Hope that was a help.

1 Like

@dontcheckme thank you it was worked.

@ankit_rupal thank you sir it was worked.

If I might intervene, there’s a quicker/shorter way to do this.
One C++ STL function many are unaware of, is the GCD one : __gcd(a,b)
Now, LCM will be a * b/ __gcd(a,b) and make sure a and b are long long ints. Hope it helps.
Yours
Tom Marvolo Riddle