Longest Consecutive Subsequence when only one insert operation is allowed
Given a sequence of positive integers of length N. The only operation allowed is to insert a single integer of any value at any position in the sequence. The task is to find the sub-sequence of maximum length that contains consecutive values in increasing order.
Examples:
Input: arr[] = {2, 1, 4, 5}
Output: 4
Insert element with value 3 at the 3rd position.(1 based indexing)
The new sequence becomes {2, 1, 3, 4, 5}
Longest consecutive sub-sequence would be {2, 3, 4, 5}
Input: arr[] = {2, 1, 2, 3, 5, 7} c
Output: 5
Approach: The idea is to use Dynamic Programming.
Let dp[val][0] be the length of required subsequence that ends in an element equal to val and the element is not inserted yet. Let dp[val][1] be the length of required subsequence that ends in an element equal to val and some element has been inserted already.
Now break the problem into its subproblems as follows:
To calculate dp[val][0], as no element in inserted, the length of the subsequence will increase by 1 from its previous value
dp[val][0] = 1 + dp[val – 1][0].
To calculate dp[val][1], consider these two cases:
- When the element is already inserted for (val-1), then there would be an increment of length 1 from dp[ val-1 ][ 1 ]
- When the element has not been inserted yet, then the element with value (val-1) can be inserted . Hence there would be an increment of length 2 from dp[ val-2 ][ 0 ].
Take maximum of both the above cases.
dp[val][1] = max(1 + dp[val – 1][1], 2 + dp[val – 2][0]).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int LongestConsSeq( int arr[], int N)
{
int maxval = 1;
for ( int i = 0; i < N; i += 1) {
maxval = max(maxval, arr[i]);
}
int dp[maxval + 1][2] = { 0 };
int ans = 1;
for ( int i = 0; i < N; i += 1) {
dp[arr[i]][0] = (1 + dp[arr[i] - 1][0]);
if (arr[i] >= 2)
dp[arr[i]][1] = max(1 + dp[arr[i] - 1][1],
2 + dp[arr[i] - 2][0]);
else
dp[arr[i]][1] = 1;
ans = max(ans, dp[arr[i]][1]);
}
return ans;
}
int main()
{
int arr[] = { 2, 1, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << LongestConsSeq(arr, N);
return 0;
}
|
Java
class GFG
{
static int LongestConsSeq( int [] arr, int N)
{
int maxval = 1 ;
for ( int i = 0 ; i < N; i += 1 )
{
maxval = Math. max(maxval, arr[i]);
}
int [][] dp = new int [maxval + 1 ][ 2 ];
int ans = 1 ;
for ( int i = 0 ; i < N; i += 1 )
{
dp[arr[i]][ 0 ] = ( 1 + dp[arr[i] - 1 ][ 0 ]);
if (arr[i] >= 2 )
dp[arr[i]][ 1 ] = Math.max( 1 + dp[arr[i] - 1 ][ 1 ],
2 + dp[arr[i] - 2 ][ 0 ]);
else
dp[arr[i]][ 1 ] = 1 ;
ans = Math.max(ans, dp[arr[i]][ 1 ]);
}
return ans;
}
public static void main (String[] args)
{
int [] arr = { 2 , 1 , 4 , 5 };
int N = arr.length;
System.out.println(LongestConsSeq(arr, N));
}
}
|
Python3
def LongestConsSeq(arr, N):
maxval = 1
for i in range (N):
maxval = max (maxval, arr[i])
dp = [[ 0 for i in range ( 2 )] for i in range (maxval + 1 )]
ans = 1
for i in range (N):
dp[arr[i]][ 0 ] = 1 + dp[arr[i] - 1 ][ 0 ]
if (arr[i] > = 2 ):
dp[arr[i]][ 1 ] = max ( 1 + dp[arr[i] - 1 ][ 1 ],
2 + dp[arr[i] - 2 ][ 0 ])
else :
dp[arr[i]][ 1 ] = 1
ans = max (ans, dp[arr[i]][ 1 ])
return ans
arr = [ 2 , 1 , 4 , 5 ]
N = len (arr)
print (LongestConsSeq(arr, N))
|
C#
using System;
class GFG
{
static int LongestConsSeq( int [] arr, int N)
{
int maxval = 1;
for ( int i = 0; i < N; i += 1)
{
maxval =Math.Max(maxval, arr[i]);
}
int [ , ] dp = new int [maxval + 1, 2];
int ans = 1;
for ( int i = 0; i < N; i += 1)
{
dp[arr[i], 0] = (1 + dp[arr[i] - 1, 0]);
if (arr[i] >= 2)
dp[arr[i], 1] = Math.Max(1 + dp[arr[i] - 1, 1],
2 + dp[arr[i] - 2, 0]);
else
dp[arr[i], 1] = 1;
ans = Math.Max(ans, dp[arr[i], 1]);
}
return ans;
}
public static void Main ()
{
int [] arr = new int [] { 2, 1, 4, 5 };
int N = arr.Length;
Console.WriteLine(LongestConsSeq(arr, N));
}
}
|
Javascript
<script>
function LongestConsSeq(arr, N)
{
let maxval = 1;
for (let i = 0; i < N; i += 1)
{
maxval = Math. max(maxval, arr[i]);
}
let dp = new Array(maxval + 1);
for (let i = 0; i < dp.length; i++)
{
dp[i] = new Array(2);
for (let j = 0; j < 2; j++)
dp[i][j] = 0;
}
let ans = 1;
for (let i = 0; i < N; i += 1)
{
dp[arr[i]][0] = (1 + dp[arr[i] - 1][0]);
if (arr[i] >= 2)
dp[arr[i]][1] = Math.max(1 + dp[arr[i] - 1][1],
2 + dp[arr[i] - 2][0]);
else
dp[arr[i]][1] = 1;
ans = Math.max(ans, dp[arr[i]][1]);
}
return ans;
}
let arr = [ 2, 1, 4, 5 ];
let N = arr.length;
document.write(LongestConsSeq(arr, N));
</script>
|
Time Complexity: O(N)
Space Complexity: O(MaxValue) where MaxValue is the maximum value present in the array.
Last Updated :
29 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...