Dado un entero positivo N > 1 , la tarea es encontrar el MCM máximo entre todos los pares (i, j) tal que i < j ≤ N .
Ejemplos:
Entrada: N = 3
Salida: 6
LCM(1, 2) = 2
LCM(1, 3) = 3
LCM(2, 3) = 6
Entrada: N = 4
Salida: 12
Enfoque: Dado que el MCM de dos elementos consecutivos es igual a sus múltiplos, es obvio que el MCM máximo será del par (N, N – 1), es decir , (N * (N – 1)) .
A continuación se muestra la implementación del enfoque anterior:
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>
Producción:
6
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)