Skip to content
Related Articles

Related Articles

Longest sub-sequence with minimum LCM
  • Last Updated : 06 Nov, 2019

Given an array arr[] of length N, the task is to find the length of the longest sub-sequence with minimum possible LCM.

Examples:

Input: arr[] = {1, 3, 1}
Output: 2
{1} and {1} are the subsequences
with the minimum possible LCM.

Input: arr[] = {3, 4, 5, 3, 2, 3}
Output: 1
{2} is the required subsequence.

Approach: The minimum possible LCM from the array will be equal to the value of the smallest element in the array. Now, to maximize the length of the resulting subsequence, find the number of elements with a value equal to this smallest value in the array and the count of these elements is the required answer.



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 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
Output:
2

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :