📜  所有元素都是斐波那契数的最大子集

📅  最后修改于: 2021-10-27 06:53:50             🧑  作者: Mango

给定一个具有正数的数组,任务是我们从包含斐波那契数列元素的数组中找到最大的子集。
在 Facebook 上询问
例子 :

Input : arr[] = {1, 4, 3, 9, 10, 13, 7};
Output : subset[] = {1, 3, 13}
The output three numbers are Fibonacci
numbers.

Input  : arr[] = {0, 2, 8, 5, 2, 1, 4, 
                  13, 23};
Output : subset[] = {0, 2, 8, 5, 2, 1, 
                    13, 23}

一个简单的解决方案是遍历给定数组的所有元素。对于每个数字,检查它是否是斐波那契数。如果是,将其添加到结果中。
下面是一个基于散列的有效解决方案

  1. 在数组中查找最大值
  2. 生成斐波那契数直到最大值并将其存储在哈希表中。
  3. 如果数字存在于哈希表中,则再次遍历数组,然后将其添加到结果中。
C++
// C++ program to find largest Fibonacci subset
#include
using namespace std;
 
// Prints largest subset of an array whose
// all elements are fibonacci numbers
void findFibSubset(int arr[], int n)
{
    // Find maximum element in arr[]
    int max = *std::max_element(arr, arr+n);
 
    // Generate all Fibonacci numbers till
    // max and store them in hash.
    int a = 0, b = 1;
    unordered_set hash;
    hash.insert(a);
    hash.insert(b);
    while (b < max)
    {
        int c = a + b;
        a = b;
        b = c;
        hash.insert(b);
    }
 
    // Npw iterate through all numbers and
    // quickly check for Fibonacci using
    // hash.
    for (int i=0; i


Java
// Java program to find
// largest Fibonacci subset
import java.util.*;
 
class GFG
{
    // Prints largest subset of an array whose
    // all elements are fibonacci numbers
    public static void findFibSubset(Integer[] x)
    {
        Integer max = Collections.max(Arrays.asList(x));
        List fib = new ArrayList();
        List result = new ArrayList();
         
        // Generate all Fibonacci numbers
        // till max and store them
        Integer a = 0;
        Integer b = 1;
        while (b < max){
            Integer c = a + b;
            a=b;
            b=c;
            fib.add(c);
        }
     
        // Now iterate through all numbers and
        // quickly check for Fibonacci
        for (Integer i = 0; i < x.length; i++){
        if(fib.contains(x[i])){
            result.add(x[i]);
        }    
        }
        System.out.println(result);
    }
 
    // Driver code
    public static void main(String args[])
    {
        Integer[] a = {4, 2, 8, 5, 20, 1, 40, 13, 23};
        findFibSubset(a);
    }
}
 
// This code is contributed by prag93


Python3
# python3 program to find largest Fibonacci subset
  
# Prints largest subset of an array whose
# all elements are fibonacci numbers
def findFibSubset(arr, n):
 
    # Find maximum element in arr[]
    m= max(arr)
  
    # Generate all Fibonacci numbers till
    # max and store them in hash.
    a = 0
    b = 1
    hash = []
    hash.append(a)
    hash.append(b)
    while (b < m):
     
        c = a + b
        a = b
        b = c
        hash.append(b)
     
  
    # Npw iterate through all numbers and
    # quickly check for Fibonacci using
    # hash.
    for i in range (n):
        if arr[i] in hash :
            print( arr[i],end=" ")
  
# Driver code
if __name__ == "__main__":
 
    arr = [4, 2, 8, 5, 20, 1, 40, 13, 23]
    n = len(arr)
    findFibSubset(arr, n)


C#
// C# program to find
// largest Fibonacci subset
using System;
using System.Linq;
using System.Collections.Generic;
     
class GFG
{
    // Prints largest subset of an array whose
    // all elements are fibonacci numbers
    public static void findFibSubset(int[] x)
    {
        int max = x.Max();
        List fib = new List();
        List result = new List();
         
        // Generate all Fibonacci numbers
        // till max and store them
        int a = 0;
        int b = 1;
        while (b < max)
        {
            int c = a + b;
            a = b;
            b = c;
            fib.Add(c);
        }
     
        // Now iterate through all numbers and
        // quickly check for Fibonacci
        for (int i = 0; i < x.Length; i++)
        {
            if(fib.Contains(x[i]))
            {
                result.Add(x[i]);
            }    
        }
        foreach(int i in result)
            Console.Write(i + " ");
    }
 
    // Driver code
    public static void Main(String []args)
    {
        int[] a = {4, 2, 8, 5, 20, 1, 40, 13, 23};
        findFibSubset(a);
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:

2 8 5 1 13 

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