Longest Increasing consecutive subsequence
Last Updated :
08 Oct, 2023
Given N elements, write a program that prints the length of the longest increasing consecutive subsequence.
Examples:
Input : a[] = {3, 10, 3, 11, 4, 5, 6, 7, 8, 12}
Output : 6
Explanation: 3, 4, 5, 6, 7, 8 is the longest increasing subsequence whose adjacent element differs by one.
Input : a[] = {6, 7, 8, 3, 4, 5, 9, 10}
Output : 5
Explanation: 6, 7, 8, 9, 10 is the longest increasing subsequence
Naive Approach: For every element, find the length of the subsequence starting from that particular element. Print the longest length of the subsequence thus formed:
C++
#include <bits/stdc++.h>
using namespace std;
int LongestSubsequence( int a[], int n)
{
int ans = 0;
for ( int i=0; i<n; i++)
{
int cnt = 1;
for ( int j=i+1; j<n; j++)
if (a[j] == (a[i]+cnt)) cnt++;
ans = max(ans, cnt);
}
return ans;
}
int main()
{
int a[] = { 3, 10, 3, 11, 4, 5, 6, 7, 8, 12 };
int n = sizeof (a) / sizeof (a[0]);
cout << LongestSubsequence(a, n);
return 0;
}
|
Java
import java.util.Scanner;
public class Main {
public static int LongestSubsequence( int a[], int n)
{
int ans = 0 ;
for ( int i= 0 ; i<n; i++)
{
int cnt = 1 ;
for ( int j=i+ 1 ; j<n; j++)
if (a[j] == (a[i]+cnt)) cnt++;
if (cnt > ans)
ans = cnt;
}
return ans;
}
public static void main(String[] args) {
int [] a = { 3 , 10 , 3 , 11 , 4 , 5 , 6 , 7 , 8 , 12 };
int n = a.length;
System.out.println(LongestSubsequence(a, n));
}
}
|
Python3
def longest_subsequence(a, n):
ans = 0
for i in range (n):
cnt = 1
for j in range (i + 1 , n):
if a[j] = = (a[i] + cnt):
cnt + = 1
ans = max (ans, cnt)
return ans
if __name__ = = "__main__" :
a = [ 3 , 10 , 3 , 11 , 4 , 5 , 6 , 7 , 8 , 12 ]
n = len (a)
print (longest_subsequence(a, n))
|
C#
using System;
class GFG
{
static int LongestSubsequence( int [] a, int n)
{
int ans = 0;
for ( int i = 0; i < n; i++)
{
int cnt = 1;
for ( int j = i + 1; j < n; j++)
{
if (a[j] == (a[i] + cnt))
{
cnt++;
}
ans = Math.Max(ans, cnt);
}
}
return ans;
}
static void Main()
{
int [] a = { 3, 10, 3, 11, 4, 5, 6, 7, 8, 12 };
int n = a.Length;
Console.WriteLine(LongestSubsequence(a, n));
}
}
|
Javascript
function LongestSubsequence(a, n)
{
let ans = 0;
for (let i=0; i<n; i++)
{
let cnt = 1;
for (let j=i+1; j<n; j++)
if (a[j] == (a[i]+cnt)) cnt++;
ans = Math.max(ans, cnt);
}
return ans;
}
let a = [ 3, 10, 3, 11, 4, 5, 6, 7, 8, 12 ];
let n = a.length;
console.log(LongestSubsequence(a, n));
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Dynamic Programming Approach: Let DP[i] store the length of the longest subsequence which ends with A[i]. For every A[i], if A[i]-1 is present in the array before i-th index, then A[i] will add to the increasing subsequence which has A[i]-1. Hence, DP[i] = DP[ index(A[i]-1) ] + 1. If A[i]-1 is not present in the array before i-th index, then DP[i]=1 since the A[i] element forms a subsequence which starts with A[i]. Hence, the relation for DP[i] is:
If A[i]-1 is present before i-th index:
- DP[i] = DP[ index(A[i]-1) ] + 1
else:
Given below is the illustration of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longestSubsequence( int a[], int n)
{
unordered_map< int , int > mp;
int dp[n];
memset (dp, 0, sizeof (dp));
int maximum = INT_MIN;
for ( int i = 0; i < n; i++) {
if (mp.find(a[i] - 1) != mp.end()) {
int lastIndex = mp[a[i] - 1] - 1;
dp[i] = 1 + dp[lastIndex];
}
else
dp[i] = 1;
mp[a[i]] = i + 1;
maximum = max(maximum, dp[i]);
}
return maximum;
}
int main()
{
int a[] = { 4, 3, 10, 3, 11, 4, 5, 6, 7, 8, 12 };
int n = sizeof (a) / sizeof (a[0]);
cout << longestSubsequence(a, n);
return 0;
}
|
Java
import java.util.*;
class lics {
static int LongIncrConseqSubseq( int arr[], int n)
{
HashMap<Integer, Integer> map = new HashMap<>();
map.put(arr[ 0 ], 1 );
for ( int i = 1 ; i < n; i++) {
if (map.containsKey(arr[i] - 1 )) {
map.put(arr[i], map.get(arr[i] - 1 ) + 1 );
map.remove(arr[i] - 1 );
}
else {
map.put(arr[i], 1 );
}
}
return Collections.max(map.values());
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int [n];
for ( int i = 0 ; i < n; i++)
arr[i] = sc.nextInt();
System.out.println(LongIncrConseqSubseq(arr, n));
}
}
|
Python3
from collections import defaultdict
import sys
def longestSubsequence(a, n):
mp = defaultdict( lambda : 0 )
dp = [ 0 for i in range (n)]
maximum = - sys.maxsize
for i in range (n):
if a[i] - 1 in mp:
lastIndex = mp[a[i] - 1 ] - 1
dp[i] = 1 + dp[lastIndex]
else :
dp[i] = 1
mp[a[i]] = i + 1
maximum = max (maximum, dp[i])
return maximum
a = [ 3 , 10 , 3 , 11 , 4 , 5 , 6 , 7 , 8 , 12 ]
n = len (a)
print (longestSubsequence(a, n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int longIncrConseqSubseq( int []arr,
int n)
{
Dictionary< int ,
int > map = new Dictionary< int ,
int >();
map.Add(arr[0], 1);
for ( int i = 1; i < n; i++)
{
if (map.ContainsKey(arr[i] - 1))
{
map.Add(arr[i], map[arr[i] - 1] + 1);
map.Remove(arr[i] - 1);
}
else
{
if (!map.ContainsKey(arr[i]))
map.Add(arr[i], 1);
}
}
int max = int .MinValue;
foreach (KeyValuePair< int ,
int > entry in map)
{
if (entry.Value > max)
{
max = entry.Value;
}
}
return max;
}
public static void Main(String []args)
{
int []arr = {3, 10, 3, 11,
4, 5, 6, 7, 8, 12};
int n = arr.Length;
Console.WriteLine(longIncrConseqSubseq(arr, n));
}
}
|
Javascript
<script>
function longestSubsequence(a, n)
{
var mp = new Map();
var dp = Array(n).fill(0);
var maximum = -1000000000;
for ( var i = 0; i < n; i++) {
if (mp.has(a[i] - 1)) {
var lastIndex = mp.get(a[i] - 1) - 1;
dp[i] = 1 + dp[lastIndex];
}
else
dp[i] = 1;
mp.set(a[i], i + 1);
maximum = Math.max(maximum, dp[i]);
}
return maximum;
}
var a = [3, 10, 3, 11, 4, 5, 6, 7, 8, 12];
var n = a.length;
document.write( longestSubsequence(a, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(N), as we are using a loop to traverse N times.
- Auxiliary Space: O(N), as we are using extra space for dp and map m.
Please Login to comment...