📜  数组中斐波那契数列的最长子序列的长度

📅  最后修改于: 2021-10-28 01:28:48             🧑  作者: Mango

给定一个包含非负整数的数组arr ,任务是打印该数组中斐波那契数列的最长子序列的长度。
例子:

方法:

  • 构建包含所有斐波那契数的哈希表,用于在 O(1) 时间内测试一个数字。
  • 现在,我们将遍历给定的数组。
  • 我们将遍历过程中遇到的所有斐波那契数列包含在最长子序列中,因此每次遇到斐波那契数时,答案加 1。
  • 一旦遇到整个初始数组,我们就有了仅包含斐波那契数列的最长子序列的长度。

下面是上述方法的实现:

C++
// C++ program to find the length
// of longest subsequence of
// Fibonacci Numbers in an Array
 
#include 
using namespace std;
#define N 100005
 
// Function to create hash table
// to check Fibonacci numbers
void createHash(set& hash,
                int maxElement)
{
    int prev = 0, curr = 1;
    hash.insert(prev);
    hash.insert(curr);
 
    while (curr <= maxElement) {
        int temp = curr + prev;
        hash.insert(temp);
        prev = curr;
        curr = temp;
    }
}
 
// Function to find the longest
// subsequence containing
// all Fibonacci numbers
int longestFibonacciSubsequence(
    int arr[], int n)
{
    set hash;
    createHash(
        hash,
        *max_element(arr, arr + n));
 
    int answer = 0;
 
    for (int i = 0; i < n; i++) {
        if (hash.find(arr[i])
            != hash.end()) {
            answer++;
        }
    }
 
    return answer;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 4, 11, 2, 9, 21 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << longestFibonacciSubsequence(arr, n)
         << endl;
 
    return 0;
}


Java
// Java program to find the length
// of longest subsequence of
// Fibonacci Numbers in an Array
import java.util.*;
 
class GFG{
static final int N = 100005;
  
// Function to create hash table
// to check Fibonacci numbers
static void createHash(HashSet hash,
                int maxElement)
{
    int prev = 0, curr = 1;
    hash.add(prev);
    hash.add(curr);
  
    while (curr <= maxElement) {
        int temp = curr + prev;
        hash.add(temp);
        prev = curr;
        curr = temp;
    }
}
  
// Function to find the longest
// subsequence containing
// all Fibonacci numbers
static int longestFibonacciSubsequence(
    int arr[], int n)
{
    HashSet hash = new HashSet();
    createHash(
        hash,Arrays.stream(arr).max().getAsInt());
  
    int answer = 0;
  
    for (int i = 0; i < n; i++) {
        if (hash.contains(arr[i])) {
            answer++;
        }
    }
  
    return answer;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 3, 4, 11, 2, 9, 21 };
    int n = arr.length;
  
    // Function call
    System.out.print(longestFibonacciSubsequence(arr, n)
         +"\n");
  
}
}
 
// This code contributed by Princi Singh


Python 3
# Python 3 program to find the length
# of longest subsequence of
# Fibonacci Numbers in an Array
 
N = 100005
 
# Function to create hash table
# to check Fibonacci numbers
def createHash(hash,maxElement):
    prev = 0
    curr = 1
    hash.add(prev)
    hash.add(curr)
 
    while (curr <= maxElement):
        temp = curr + prev
        hash.add(temp)
        prev = curr
        curr = temp
     
# Function to find the longest
# subsequence containing
# all Fibonacci numbers
def longestFibonacciSubsequence(arr, n):
    hash = set()
    createHash(hash,max(arr))
 
    answer = 0
 
    for i in range(n):
        if (arr[i] in hash):
            answer += 1
 
    return answer
 
# Driver code
if __name__ == '__main__':
    arr = [3, 4, 11, 2, 9, 21]
    n = len(arr)
 
    # Function call
    print(longestFibonacciSubsequence(arr, n))
 
# This code is contributed by Surendra_Gangwar


C#
// C# program to find the length
// of longest subsequence of
// Fibonacci Numbers in an Array
using System;
using System.Linq;
using System.Collections.Generic;
 
class GFG{
static readonly int N = 100005;
   
// Function to create hash table
// to check Fibonacci numbers
static void createHash(HashSet hash,
                int maxElement)
{
    int prev = 0, curr = 1;
    hash.Add(prev);
    hash.Add(curr);
   
    while (curr <= maxElement) {
        int temp = curr + prev;
        hash.Add(temp);
        prev = curr;
        curr = temp;
    }
}
   
// Function to find the longest
// subsequence containing
// all Fibonacci numbers
static int longestFibonacciSubsequence(
    int []arr, int n)
{
    HashSet hash = new HashSet();
    createHash(hash,arr.Max());
   
    int answer = 0;
   
    for (int i = 0; i < n; i++) {
        if (hash.Contains(arr[i])) {
            answer++;
        }
    }
   
    return answer;
}
   
// Driver code
public static void Main(String[] args)
{
    int []arr = { 3, 4, 11, 2, 9, 21 };
    int n = arr.Length;
   
    // Function call
    Console.Write(longestFibonacciSubsequence(arr, n)
         +"\n");
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
3

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程