Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Maximum LCM among all pairs (i, j) of first N natural numbers

  • Last Updated : 01 Apr, 2021

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:
LCM(1, 2) = 2 
LCM(1, 3) = 3 
LCM(2, 3) = 6
Input: N = 4 
Output: 12 
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

 



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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the maximum LCM
// among all the pairs(i, j) of
// first n natural numbers
int maxLCM(int n)
{
    return (n * (n - 1));
}
 
// Driver code
int main()
{
    int n = 3;
 
    cout << maxLCM(n);
 
    return 0;
}

Java




// Java implementation of the approach
class GFG
{
     
// Function to return the maximum LCM
// among all the pairs(i, j) of
// first n natural numbers
static int maxLCM(int n)
{
    return (n * (n - 1));
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3;
 
    System.out.println(maxLCM(n));
}
}
 
// This code is contributed by Code_Mech

Python3




# Python3 implementation of the approach
 
# Function to return the maximum LCM
# among all the pairs(i, j) of
# first n natural numbers
def maxLCM(n) :
 
    return (n * (n - 1));
 
# Driver code
if __name__ == "__main__" :
 
    n = 3;
 
    print(maxLCM(n));
 
# This code is contributed by AnkitRai01

C#




// C# implementation of the approach
using System;
     
class GFG
{
     
// Function to return the maximum LCM
// among all the pairs(i, j) of
// first n natural numbers
static int maxLCM(int n)
{
    return (n * (n - 1));
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3;
 
    Console.WriteLine(maxLCM(n));
}
}
 
// This code is contributed by Rajput-Ji

Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the maximum LCM
// among all the pairs(i, j) of
// first n natural numbers
function maxLCM(n)
{
    return (n * (n - 1));
}
 
// Driver code
var n = 3;
document.write(maxLCM(n));
 
</script>
Output: 
6

 

Time Complexity: O(1)
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :