📜  包含卢卡斯数的数组的最长子序列

📅  最后修改于: 2021-05-07 01:41:58             🧑  作者: Mango

给定N个元素的数组arr [] ,任务是找到arr []中最长子序列的长度,以使序列中的所有元素都是卢卡斯数。

例子:

方法:

  • 在数组中找到最大元素。
  • 生成最大卢卡斯数,并将它们存储在一组中。
  • 遍历数组arr []并检查当前元素是否存在于集合中。
  • 如果它存在于集合中,则增加计数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the length of
// the longest required sub-sequence
int LucasSequence(int arr[], int n)
{
    // Find the maximum element from 
    // the array
    int max = *max_element(arr, arr+n);
  
    // Insert all lucas numbers
    // below max to the set
    // a and b are first two elements
    // of the Lucas sequence
    unordered_set s;
    int a = 2, b = 1, c;
    s.insert(a);
    s.insert(b);
    while (b < max) {
        int c = a + b;
        a = b;
        b = c;
        s.insert(b);
    }
  
    int count = 0;
    for (int i = 0; i < n; i++) {
  
        // If current element is a Lucas 
        // number, increment count
        auto it = s.find(arr[i]);
        if (it != s.end()) 
            count++;
    }
  
    // Return the count
    return count;
}
  
// Driver code
int main()
{
    int arr[] = { 7, 11, 22, 4, 2, 1, 8, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    cout << LucasSequence(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
  
    // Function to return the length of 
    // the longest required sub-sequence 
    static int LucasSequence(int[] arr, int n)
    {
        // Find the maximum element from 
        // the array 
        int max = Arrays.stream(arr).max().getAsInt();
        int counter = 0;
  
        // Insert all lucas numbers 
        // below max to the set 
        // a and b are first two elements 
        // of the Lucas sequence 
        HashSet s = new HashSet<>();
  
        int a = 2, b = 1;
        s.add(a);
        s.add(b);
  
        while (b < max)
        {
            int c = a + b;
            a = b;
            b = c;
            s.add(b);
        }
  
        for (int i = 0; i < n; i++)
        {
  
            // If current element is a Lucas 
            // number, increment count 
            if (s.contains(arr[i])) 
            {
                counter++;
            }
        }
  
        // Return the count 
        return counter;
    }
  
    // Driver code 
    public static void main(String[] args)
    {
        int[] arr = {7, 11, 22, 4, 2, 1, 8, 9};
        int n = arr.length;
  
        System.out.println(LucasSequence(arr, n));
    }
}
  
// This code has been contributed by 29AjayKumar


Python3
# Python 3 implementation of the approach
  
# Function to return the length of
# the longest required sub-sequence
def LucasSequence(arr, n):
      
    # Find the maximum element from 
    # the array
    max = arr[0]
    for i in range(len(arr)):
        if(arr[i] > max):
            max = arr[i]
  
    # Insert all lucas numbers below max 
    # to the set a and b are first two 
    # elements of the Lucas sequence
    s = set()
    a = 2
    b = 1
    s.add(a)
    s.add(b)
    while (b < max):
        c = a + b
        a = b
        b = c
        s.add(b)
  
    count = 0
    for i in range(n):
          
        # If current element is a Lucas 
        # number, increment count
        if(arr[i] in s):
            count += 1
  
    # Return the count
    return count
  
# Driver code
if __name__ == '__main__':
    arr = [7, 11, 22, 4, 2, 1, 8, 9]
    n = len(arr)
  
    print(LucasSequence(arr, n))
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach 
using System;
using System.Collections.Generic;
using System.Linq;
  
class GFG
{
      
    // Function to return the length of 
    // the longest required sub-sequence 
    static int LucasSequence(int []arr, int n) 
    { 
        // Find the maximum element from 
        // the array 
        int max = arr.Max(); 
        int counter = 0; 
  
        // Insert all lucas numbers 
        // below max to the set 
        // a and b are first two elements 
        // of the Lucas sequence 
        HashSet s = new HashSet() ;
          
        int a = 2, b = 1 ;
        s.Add(a); 
        s.Add(b); 
          
        while (b < max) 
        { 
            int c = a + b; 
            a = b; 
            b = c; 
            s.Add(b); 
        } 
      
        for (int i = 0; i < n; i++) 
        { 
      
            // If current element is a Lucas 
            // number, increment count 
            if (s.Contains(arr[i]))
                counter++; 
        } 
      
        // Return the count 
        return counter; 
    } 
  
    // Driver code 
    static public void Main() 
    { 
        int []arr = { 7, 11, 22, 4, 2, 1, 8, 9 }; 
        int n = arr.Length ;
      
        Console.WriteLine(LucasSequence(arr, n)) ;
    } 
}
  
// This code is contributed by Ryuga


输出:
5