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.
Input: N = 3
LCM(1, 2) = 2
LCM(1, 3) = 3
LCM(2, 3) = 6
Input: N = 4
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.
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:
Time Complexity: O(1)