Given a strictly increasing array A of positive integers where,
. The task is to find the length of the longest Fibonacci-like subsequence of A. If such subsequence does not exist, return 0.
Input: A = [1, 3, 7, 11, 12, 14, 18]
The longest subsequence that is Fibonacci-like: [1, 11, 12]. Other possible subsequences are [3, 11, 14] or [7, 11, 18].
Input: A = [1, 2, 3, 4, 5, 6, 7, 8]
The longest subsequence that is Fibonacci-like: [1, 2, 3, 5, 8].
Naive Approach: A Fibonacci-like sequence is such that it has each two adjacent terms that determine the next expected term.
For example, with 1, 1, we expect that the sequence must continue 2, 3, 5, 8, 13, … and so on.
- Use Set or Map to determine quickly whether the next term of Fibonacci sequence is present in the array A or not. Because of the exponential growth of these terms, there will be not more than log(M) searches to get next element on each iteration.
- For each starting pair A[i], A[j], we maintain the next expected value y = A[i] + A[j] and the previously seen largest value x = A[j]. If y is in the array, then we can then update these values (x, y) -> (y, x+y) otherwise we stop immediately.
Below is the implementation of above approach:
Time Complexity: O(N2 * log(M)), where N is the length of array and M is max(A).
Efficient Approach: To optimize the above approach the idea is to implement Dynamic Programming. Initialize a dp table, dp[a, b] that represents the length of Fibonacci sequence ends up with (a, b). Then update the table as dp[a, b] = (dp[b – a, a] + 1 ) or 2
Below is the implementation of the above approach:
Time Complexity: O(N2), where N is the length of the array.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the longest Fibonacci-like subarray of the given array
- Length of longest subsequence of Fibonacci Numbers in an Array
- Check if the n-th term is odd or even in a Fibonacci like sequence
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Length of longest Fibonacci subarray formed by removing only one element
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Check if a M-th fibonacci number divides N-th fibonacci number
- Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
- Length of longest strict bitonic subsequence
- Length of the longest subsequence consisting of distinct elements
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Length of Longest Prime Subsequence in an Array
- Length of longest Powerful number subsequence in an Array
- Length of Longest Perfect number Subsequence in an Array
- Length of longest increasing index dividing subsequence
- Length of longest subsequence in an Array having all elements as Nude Numbers
- Length of longest subsequence whose XOR value is odd
- Maximize length of longest increasing prime subsequence from the given array
- Length of longest increasing prime subsequence from a given array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.