📜  最长子序列,最大GCD

📅  最后修改于: 2021-04-22 07:07:50             🧑  作者: Mango

给定长度为N的数组arr [] ,任务是找到具有最大可能GCD的最长子序列的长度。

例子:

方法:来自数组的最大可能GCD将等于数组中最大元素的值。现在,要使所得子序列的长度最大,请在数组中找到值等于此最大值的元素数量,并且这些元素的计数是必需的答案。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the length
// of the largest subsequence with
// maximum possible GCD
int maxLen(int* arr, int n)
{
    // Maximum value from the array
    int max_val = *max_element(arr, arr + n);
  
    // To store the frequency of the
    // maximum element in the array
    int freq = 0;
  
    for (int i = 0; i < n; i++) {
  
        // If current element is equal
        // to the maximum element
        if (arr[i] == max_val)
            freq++;
    }
  
    return freq;
}
  
// Driver code
int main()
{
    int arr[] = { 3, 2, 2, 3, 3, 3 };
    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
// maximum possible GCD
static int maxLen(int[] arr, int n)
{
    // Maximum value from the array
    int max_val = Arrays.stream(arr).max().getAsInt();
  
    // To store the frequency of the
    // maximum element in the array
    int freq = 0;
  
    for (int i = 0; i < n; i++) 
    {
  
        // If current element is equal
        // to the maximum element
        if (arr[i] == max_val)
            freq++;
    }
    return freq;
}
  
// Driver code
public static void main(String []args) 
{
    int arr[] = { 3, 2, 2, 3, 3, 3 };
    int n = arr.length;
  
    System.out.println(maxLen(arr, n));
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
  
# Function to return the length 
# of the largest subsequence with 
# maximum possible GCD 
def maxLen(arr, n) :
      
    # Maximum value from the array 
    max_val = max(arr); 
  
    # To store the frequency of the 
    # maximum element in the array 
    freq = 0; 
  
    for i in range(n) :
  
        # If current element is equal 
        # to the maximum element 
        if (arr[i] == max_val) :
            freq += 1; 
  
    return freq; 
  
# Driver code 
if __name__ == "__main__" : 
  
    arr = [ 3, 2, 2, 3, 3, 3 ]; 
    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
// maximum possible GCD
static int maxLen(int[] arr, int n)
{
    // Maximum value from the array
    int max_val = arr.Max();
  
    // To store the frequency of the
    // maximum element in the array
    int freq = 0;
  
    for (int i = 0; i < n; i++) 
    {
  
        // If current element is equal
        // to the maximum element
        if (arr[i] == max_val)
            freq++;
    }
    return freq;
}
  
// Driver code
public static void Main(String []args) 
{
    int []arr = { 3, 2, 2, 3, 3, 3 };
    int n = arr.Length;
  
    Console.WriteLine(maxLen(arr, n));
}
}
  
// This code is contributed by PrinciRaj1992


输出:
4