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]
Explanation: 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]
Explanation: The longest subsequence that is Fibonacci-like: [1, 2, 3, 5, 8].
A Fibonacci-like sequence is such that it has each two adjacent terms that determines the next expected term. For example, with 1, 1, we expect that the sequence must continue 2, 3, 5, 8, 13, … and so on.
We will 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).
- Length of longest strict bitonic subsequence
- Find the longest Fibonacci-like subarray of the given array
- Find the Increasing subsequence of length three with maximum product
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Longest subsequence with no 0 after 1
- Longest Zig-Zag Subsequence
- Longest subsequence whose average is less than K
- Longest Bitonic Subsequence in O(n log n)
- Longest Increasing Odd Even Subsequence
- Longest Consecutive Subsequence
- Maximum length subsequence possible of the form R^N K^N
- Longest subsequence having equal numbers of 0 and 1
- Longest Common Increasing Subsequence (LCS + LIS)
- Longest Increasing consecutive subsequence
- Construction of Longest Increasing Subsequence (N log N)