Largest Fibonacci Subsequence | Practice | GeeksforGeeks

Given an array with positive integers, the task is to find the largest subsequence from the array which contains only Fibonacci numbers.
 
Example 1:
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}


Your Task:  
You don't need to read input or print anything. Your task is to complete the function findFibSubset() which takes the array A[] and its size N as inputs and returns the  elements of the fibonacci subsequence in a vector. If no such number found return empty vector.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
1<=N<=103
1<=A[]<=109
 
0
bharathimp19932 weeks ago
Java solution
 
 public int[] findFibSubset(int arr[], int n)   {       List<Integer> list =new ArrayList<>();      for(int i=0;i<arr.length;i++){          if(checkFib(arr[i]))                list.add(arr[i]);      }            int sub[] = new int[list.size()];            for(int i=0;i<list.size();i++){          sub[i] = list.get(i);      }            return sub;   }         private boolean checkFib(long n){       long a=0;       long b=1;       long c= a+b;       long prev = 0;       while(c<=n){           prev = c;           c = a+b;           a=b;           b=c;       }              return (prev == n);          }
0
zhyuanshan3 weeks ago
class Solution{
    unordered_set<int> fibonacciNumbers;
    int a = 1, b = 1;
    bool isFibonacci(int n){
        if(n == 1) return true;
        if(fibonacciNumbers.find(n) != fibonacciNumbers.end()) return true;
        
        while(b < n){
            int temp = b;
            b = a + b;
            a = temp;
            
            fibonacciNumbers.insert(b);
            if(b == n) return true;
        }
        
        return false;
    }
    public:
    vector<int> findFibSubset(int arr[], int n) {
        vector<int> result;
        for(int i = 0; i < n; i++){
            if(isFibonacci(arr[i])){
                result.push_back(arr[i]);
            }
        }
        
        return result;
    }
};
0
akkeshri140420012 months ago
bool isfib(int n){
       int a=0,b=1,c=1;
       while(c<=n){
           if(c==n){
               return 1;
           }
           a=b;
           b=c;
           c=a+b;
       }
       return 0;
   }
   vector<int> findFibSubset(int arr[], int n) {
       vector<int>v1;
       
       for(int i=0;i<n;i++){
           if(isfib(arr[i])){
           
               v1.push_back(arr[i]);
           }
       }
       return v1;
       }
   
       
     
0
akkeshri14042001
This comment was deleted.
0
ayushagarwal88202 months ago
JAVA SIMPLEST SOLUTION 
 
 public int[] findFibSubset(int arr[], int n)   {       int x=0;       ArrayList<Integer> al=new ArrayList<>();              for(int i=0;i<n;i++){           if(fibo(arr[i])){               al.add(arr[i]);           }       }       int[] subset=new int[al.size()];       for(int i=0;i<al.size();i++){           subset[i]=al.get(i);       }       return subset;   }      public static boolean fibo(int n){       int a=0;       int b=1;       int c=0;       if(n==a || n==b){           return true;       }       while(c<=n){           c=a+b;           if(c==n){               return true;           }           a=b;           b=c;       }       return false;   }
+1
avinav26113 months ago
Easy C++ Solution (0.4/1.8s)
 
+2
raunakmishra12433 months ago
 vector<int> findFibSubset(int arr[], int n) 
    {
          vector<int>v;
          int  k=*max_element(arr,arr+n);
          unordered_map<int,int>m;
          int a=0,b=1,c=0;
        while(c<=k)
         {
        m[c]++;
        c=a+b;
        a=b;
        b=c;
    
         }
    
          for(int i=0;i<n;i++)
          {
             if(m.find(arr[i])!=m.end())
             v.push_back(arr[i]);
          }
    
          return v;
    }
-1
imranwahid6 months ago
Easy C++ solution
0
AlphaCoder8 months ago
AlphaCoder
The solution to this example is incorrect either, in the question they have mentioned that the expected space complexity is O(N) which means we can store up to 10^3 numbers. But the solution says that we need to generate Fibonacci numbers up to the max number in the array which means that the space complexity can be around O(max(Arr[])) which is incorrect.
The appropriate solution is :
class Solution{    public:    vector<int> findFibSubset(int arr[], int n) {        vector<int> res;        for(int i=0;i<n;i++){ unsigned="" long="" long="" int="" v1="5*((unsigned" long="" long="" int)arr[i]*arr[i])+4;="" unsigned="" long="" long="" int="" v2="5*((unsigned" long="" long="" int)arr[i]*arr[i])-4;="" cout<<v1<<"="" "<<v2<<endl;="" if(isperfectsquare(v1)||isperfectsquare(v2)){="" res.push_back(arr[i]);="" }="" }="" return="" res;="" }="" bool="" isperfectsquare(unsigned="" long="" long="" int="" n){="" cout<<n<<endl;="" unsigned="" long="" long="" int="" sq="sqrt(n);" cout<<sq<<endl;="" return="" (sq*sq="=n);" }="" };="">
0
Din Djarin9 months ago
Din Djarin
Binet formula won't work here
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.