Dada una array arr[] de longitud N , la tarea es encontrar la longitud de la subsecuencia más larga con el mínimo LCM posible .
Ejemplos:
Entrada: arr[] = {1, 3, 1}
Salida: 2
{1} y {1} son las subsecuencias
con el mínimo LCM posible.
Entrada: arr[] = {3, 4, 5, 3, 2, 3}
Salida: 1
{2} es la subsecuencia requerida.
Enfoque: El LCM mínimo posible de la array será igual al valor del elemento más pequeño de la array. Ahora, para maximizar la longitud de la subsecuencia resultante, busque el número de elementos con un valor igual a este valor más pequeño en la array y el recuento de estos elementos es la respuesta requerida.
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 length // of the largest subsequence with // minimum possible LCM int maxLen(int* arr, int n) { // Minimum value from the array int min_val = *min_element(arr, arr + n); // To store the frequency of the // minimum element in the array int freq = 0; for (int i = 0; i < n; i++) { // If current element is equal // to the minimum element if (arr[i] == min_val) freq++; } return freq; } // Driver code int main() { int arr[] = { 1, 3, 1 }; int n = sizeof(arr) / sizeof(int); cout << maxLen(arr, n); return 0; }
Java
// Java implementation of the approach import java.util.Arrays; class GFG { // Function to return the length // of the largest subsequence with // minimum possible LCM static int maxLen(int[] arr, int n) { // Minimum value from the array int min_val = Arrays.stream(arr).min().getAsInt(); // To store the frequency of the // minimum element in the array int freq = 0; for (int i = 0; i < n; i++) { // If current element is equal // to the minimum element if (arr[i] == min_val) freq++; } return freq; } // Driver code public static void main(String []args) { int arr[] = { 1, 3, 1 }; int n = arr.length; System.out.println(maxLen(arr, n)); } } // This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of the approach # Function to return the length # of the largest subsequence with # minimum possible LCM def maxLen(arr, n) : # Minimum value from the array min_val = min(arr); # To store the frequency of the # minimum element in the array freq = 0; for i in range(n) : # If current element is equal # to the minimum element if (arr[i] == min_val) : freq += 1; return freq; # Driver code if __name__ == "__main__" : arr = [ 1, 3, 1 ]; n = len(arr); print(maxLen(arr, n)); # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; using System.Linq; class GFG { // Function to return the length // of the largest subsequence with // minimum possible LCM static int maxLen(int[] arr, int n) { // Minimum value from the array int min_val = arr.Min(); // To store the frequency of the // minimum element in the array int freq = 0; for (int i = 0; i < n; i++) { // If current element is equal // to the minimum element if (arr[i] == min_val) freq++; } return freq; } // Driver code public static void Main(String []args) { int []arr = { 1, 3, 1 }; int n = arr.Length; Console.WriteLine(maxLen(arr, n)); } } // This code is contributed by 29AjayKumar
Javascript
<script> // Javascript implementation of the approach // Function to return the length // of the largest subsequence with // minimum possible LCM function maxLen(arr, n) { // Minimum value from the array var min_val = arr.reduce((a, b) => Math.min(a,b)) // To store the frequency of the // minimum element in the array var freq = 0; for (var i = 0; i < n; i++) { // If current element is equal // to the minimum element if (arr[i] == min_val) freq++; } return freq; } // Driver code var arr = [ 1, 3, 1 ]; var n = arr.length; document.write( maxLen(arr, n)); // This code is contributed by itsok. </script>
2
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por DivyanshuShekhar1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA