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

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

`// 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time Complexity:** O(1)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Number of pairs from the first N natural numbers whose sum is divisible by K
- Count pairs of natural numbers with GCD equal to given number
- Find maximum N such that the sum of square of first N natural numbers is not more than X
- Find the permutation of first N natural numbers such that sum of i % P
_{i}is maximum possible - Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Natural Numbers
- LCM of First n Natural Numbers
- Sum of first n natural numbers
- Sum of first N natural numbers which are divisible by 2 and 7
- Sum of cubes of even and odd natural numbers
- Sum of cubes of first n odd natural numbers
- Sum of squares of first n natural numbers
- Average of first n even natural numbers
- Sum of all odd natural numbers in range L and R
- Sum of first N natural numbers which are not powers of K
- Sum of first N natural numbers which are divisible by X or Y
- Sum of squares of first n natural numbers
- Repeated sum of first N natural numbers
- Sum of fifth powers of the first n natural numbers
- Sum of all natural numbers in range L to R

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.