Open In App

Find maximum LCM that can be obtained from four numbers less than or equal to N

Given an integer, N, the task is to find the maximum LCM (Least Common Multiple) that can be obtained from four numbers less than or equal to N (4 ? N).

Note: Duplicate numbers can be used.



Examples:

Input: N = 4
Output: 12
Explanation: The four numbers can be [4, 4, 3, 2] or [4, 4, 4, 3]. It can be shown that 12 is the maximum LCM of four numbers that can be obtained from numbers less than or equal to 4.



Input: N = 5
Output: 60
Explanation: The four numbers can be [5, 5, 4, 3] or [5, 4, 3, 2], etc. 60 is the maximum that can be obtained.

Approach: This problem can be solved using Greedy Algorithm:

Since we want to maximize LCM we need to think of numbers such that they are coprime with each other as LCM of coprime numbers is maximum as they have no factor common except 1.
LCM = product of numbers / GCD 
To maximize LCM we need to reduce GCD, thus choosing coprime numbers where GCD is 1 is the best option.

Below is the implementation of the above approach:




// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
 
long long maxLCM(long long N)
{
  if (N <= 2)
    return N;
  if (N == 3)
    return 6;
  if (N == 4)
    return 12;
  long long ans = N;
  long long i = N - 1;
  int cnt = 1;
  while (i > 1 and cnt < 4) {
    if (__gcd(ans, i) == 1) {
      cnt++;
      ans *= i;
    }
    i--;
  }
  if ((N % 2) == 0)
    ans = max(ans, maxLCM(N - 1));
  return ans;
}
 
// Driver code
int main()
{
  int N = 5;
  int M = 4;
 
  // Function Call
  cout << maxLCM(N) << endl;
  cout << maxLCM(M) << endl;
  return 0;
}




// Java code for the above approach:
 
import java.io.*;
 
class GFG {
 
    static long maxLCM(long N)
    {
        if (N <= 2) {
            return N;
        }
        if (N == 3) {
            return 6;
        }
        if (N == 4) {
            return 12;
        }
        long ans = N;
        long i = N - 1;
        int cnt = 1;
        while (i > 1 && cnt < 4) {
            if (gcd(ans, i) == 1) {
                cnt++;
                ans *= i;
            }
            i--;
        }
        if ((N % 2) == 0) {
            ans = Math.max(ans, maxLCM(N - 1));
        }
        return ans;
    }
 
    // gcd method
    static long gcd(long a, long b)
    {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }
 
    public static void main(String[] args)
    {
        long N = 5;
        long M = 4;
 
        // Function Call
        System.out.println(maxLCM(N));
        System.out.println(maxLCM(M));
    }
}
 
// This code is contributed by lokesh.




# Python3 code for the above approach
import math
 
# This Function will returns the maximum value of the
# LCM of any four integers less than or equal to N
def maxLCM(N):
    if N <= 2:
        return N
    if N == 3:
        return 6
    if N == 4:
        return 12
    ans = N
    i = N - 1
    cnt = 1
    while i > 1 and cnt < 4:
        if math.gcd(ans, i) == 1:
            cnt += 1
            ans *= i
        i -= 1
    if N % 2 == 0:
        ans = max(ans, maxLCM(N - 1))
    return ans
 
# Driver code
N = 5
M = 4
 
# Function Call
print(maxLCM(N))
print(maxLCM(M))
 
#This code is contributed by nikhilsainiofficial546




// C# code for the above approach:
using System;
 
public class GFG{
 
  static long maxLCM(long N)
  {
    if (N <= 2) {
      return N;
    }
    if (N == 3) {
      return 6;
    }
    if (N == 4) {
      return 12;
    }
    long ans = N;
    long i = N - 1;
    int cnt = 1;
    while (i > 1 && cnt < 4) {
      if (gcd(ans, i) == 1) {
        cnt++;
        ans *= i;
      }
      i--;
    }
    if ((N % 2) == 0) {
      ans = Math.Max(ans, maxLCM(N - 1));
    }
    return ans;
  }
 
  // gcd method
  static long gcd(long a, long b)
  {
    if (b == 0) {
      return a;
    }
    return gcd(b, a % b);
  }
 
  static public void Main (){
 
    // Code
    long N = 5;
    long M = 4;
 
    // Function Call
    Console.WriteLine(maxLCM(N));
    Console.WriteLine(maxLCM(M));
  }
}
 
// This code is contributed by lokeshmvs21.




// Javascript code for the above approach:
function gcd(a, b){
    
  // Everything divides 0
  if(b == 0){
    return a;
  }
    
  return gcd(b, a % b);
}
 
function maxLCM( N)
{
  if (N <= 2)
    return N;
  if (N == 3)
    return 6;
  if (N == 4)
    return 12;
  let ans = N;
  let i = N - 1;
  let cnt = 1;
  while (i > 1 && cnt < 4) {
    if (gcd(ans, i) == 1) {
      cnt++;
      ans *= i;
    }
    i--;
  }
  if ((N % 2) == 0)
    ans = Math.max(ans, maxLCM(N - 1));
  return ans;
}
 
// Driver code
let N = 5;
let M = 4;
 
// Function Call
console.log(maxLCM(N));
console.log(maxLCM(M));
 
// This code is contributed by poojaagarwal2.

Output
60
12

Time Complexity: O(log(max(N)))
Auxiliary Space: O(1)

Related Articles:


Article Tags :