Hints:

Approach: Since the task is to maximize the LCM, so if all three numbers don’t have any common factor then the LCM will be the product of those three numbers and that will be maximum.


  • If n is odd then the answer will be n, n-1, n-2.
  • If n is even,
    1. If gcd of n and n-3 is 1 then answer will be n, n-1, n-3.
    2. Otherwise, n-1, n-2, n-3 will be required answer.
Solution  in C++:
  1. ///**********ALLAH IS ALMIGHTY************///
  2. ///AH Tonmoy
  3. ///Department of CSE,23rd Batch
  4. ///Islamic University,Bangladesh
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. void mlcm(long long int n)
  8. { if (n % 2 != 0)
  9. cout << n*(n - 1)*(n - 2);
  10. else if (__gcd(n, (n - 3)) == 1)
  11. cout << n*(n - 1)*(n - 3);
  12. else
  13. cout <<(n-1)*(n - 2)*(n - 3);
  14. }
  15.  
  16. int main()
  17. {
  18. long long int n;
  19. cin>>n;
  20. if(n<3)
  21. cout<<n<<endl;
  22. else
  23. mlcm(n);
  24. return 0;
  25. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.