Longest Prefix Subsequence matching Fibonacci Sequence
Last Updated :
04 Dec, 2023
Given an array arr of size N. Also, there is another array B of the infinite size where B[0] = B[1] = 1, and for all (i >= 2), B[i] = B[i-1]+B[i-2], the task is to find the length of the longest subsequence which is the prefix of array B. If there is no subsequence which is the prefix of array B then return 0.
Examples:
Input: N = 6, arr = {1, 2, 3, 1, 2, 3}
Output: 4
Explanation: Subsequence {1,1,2,3} which is a prefix array of B of length 4.
Input: N = 5, arr = {2, 3, 1, 2, 5}
Output: 1
Explanation: Subsequence {1} which is a prefix array of B of length 1.
Approach: To solve the problem follow the below idea:
Using Dynamic Programming, we can iterate over both the arrays A and B, and see the total common length until A length is finished.
Below are the steps involved:
- Create an array dp as B of length A where each element is as dp[i] = dp[i – 1] + dp[i-2].
- Iterate over both the arrays:
- If(A[i] == B[j])
- count++, increase the lcs.
- Otherwise, increase i++, As the prefix of B should be matched.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int solve( int N, int A[])
{
vector< int > dp(N + 1);
dp[0] = 1;
dp[1] = 1;
for ( int i = 2; i <= N; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
int count = 0, j = 0;
for ( int i = 0; i < N; i++) {
if (A[i] == dp[j]) {
j++;
count++;
}
if (j == dp.size()) {
dp[i] = dp[i - 1] + dp[i - 2];
}
}
return count;
}
int main()
{
int N = 6;
int A[] = { 1, 2, 3, 1, 2, 3 };
cout << solve(N, A);
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
static int solve( int N, int [] A) {
int [] dp = new int [N + 1 ];
dp[ 0 ] = 1 ;
dp[ 1 ] = 1 ;
for ( int i = 2 ; i <= N; i++) {
dp[i] = dp[i - 1 ] + dp[i - 2 ];
}
int count = 0 , j = 0 ;
for ( int i = 0 ; i < N; i++) {
if (A[i] == dp[j]) {
j++;
count++;
}
if (j == dp.length) {
dp[i] = dp[i - 1 ] + dp[i - 2 ];
}
}
return count;
}
public static void main(String[] args) {
int N = 6 ;
int [] A = { 1 , 2 , 3 , 1 , 2 , 3 };
System.out.println(solve(N, A));
}
}
|
Python3
def solve(N, A):
dp = [ 0 ] * (N + 1 )
dp[ 0 ] = 1
dp[ 1 ] = 1
for i in range ( 2 , N + 1 ):
dp[i] = dp[i - 1 ] + dp[i - 2 ]
count = 0
j = 0
for i in range (N):
if A[i] = = dp[j]:
j + = 1
count + = 1
if j = = len (dp):
dp.append(dp[ - 1 ] + dp[ - 2 ])
return count
if __name__ = = "__main__" :
N = 6
A = [ 1 , 2 , 3 , 1 , 2 , 3 ]
print (solve(N, A))
|
C#
using System;
public class GFG {
static int solve( int N, int [] A)
{
int [] dp = new int [N + 1];
dp[0] = 1;
dp[1] = 1;
for ( int i = 2; i <= N; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
int count = 0, j = 0;
for ( int i = 0; i < N; i++) {
if (A[i] == dp[j]) {
j++;
count++;
}
if (j == dp.Length) {
dp[i] = dp[i - 1] + dp[i - 2];
}
}
return count;
}
public static void Main()
{
int N = 6;
int [] A = { 1, 2, 3, 1, 2, 3 };
Console.WriteLine(solve(N, A));
}
}
|
Javascript
function solve(N, A) {
let dp = new Array(N + 1).fill(0);
dp[0] = 1;
dp[1] = 1;
for (let i = 2; i <= N; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
let count = 0, j = 0;
for (let i = 0; i < N; i++) {
if (A[i] == dp[j]) {
j++;
count++;
}
if (j == dp.length) {
dp[i] = dp[i - 1] + dp[i - 2];
}
}
return count;
}
let N = 6;
let A = [1, 2, 3, 1, 2, 3];
console.log(solve(N, A));
|
Time Complexity: O (N + M)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...