Maximum LCM among all pairs (i, j) of first N natural numbers
Last Updated :
10 Mar, 2022
Given a positive integer N > 1, the task is to find the maximum LCM among all the pairs (i, j) such that i < j ? N.
Examples:
Input: N = 3
Output: 6
LCM(1, 2) = 2
LCM(1, 3) = 3
LCM(2, 3) = 6
Input: N = 4
Output: 12
Approach: Since the LCM of two consecutive elements is equal to their multiples then it is obvious that the maximum LCM will be of the pair (N, N – 1) i.e. (N * (N – 1)).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxLCM( int n)
{
return (n * (n - 1));
}
int main()
{
int n = 3;
cout << maxLCM(n);
return 0;
}
|
Java
class GFG
{
static int maxLCM( int n)
{
return (n * (n - 1 ));
}
public static void main(String[] args)
{
int n = 3 ;
System.out.println(maxLCM(n));
}
}
|
Python3
def maxLCM(n) :
return (n * (n - 1 ));
if __name__ = = "__main__" :
n = 3 ;
print (maxLCM(n));
|
C#
using System;
class GFG
{
static int maxLCM( int n)
{
return (n * (n - 1));
}
public static void Main(String[] args)
{
int n = 3;
Console.WriteLine(maxLCM(n));
}
}
|
Javascript
<script>
function maxLCM(n)
{
return (n * (n - 1));
}
var n = 3;
document.write(maxLCM(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...