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 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.