Longest Increasing consecutive subsequence | Set-2
Last Updated :
24 Feb, 2022
Given an array arr[] of N elements, the task is to find the length of the longest increasing subsequence whose adjacent element difference is one.
Examples:
Input: arr[] = {3, 10, 3, 11, 4, 5, 6, 7, 8, 12}
Output: 6
Explanation: The subsequence {3, 4, 5, 6, 7, 8} is the longest increasing subsequence whose adjacent elements differs by one.
Input: arr[] = {6, 7, 8, 3, 4, 5, 9, 10}
Output: 5
Approach: The naive and the dynamic programming approach has already been discussed in this article. This article discusses a simpler and easy to implement approach using Hashing. The idea is to store the length of subsequence ending with the current integer X in an unordered map m. Hence, if X + 1 occurs during the traversal, the length of the subsequence will be m[X] + 1. The maximum value in the map m is the required answer.
Below is the implementation of the above approach:
C++
// C++ program of the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the longest
// increasing consecutive subsequence
int longestSubsequence(int arr[], int N)
{
// Stores the length of longest
// subsequence qnding with key
unordered_map<int, int> m;
// Stores the required answer
int ans = 0;
// Loop to traverse array
for (int i = 0; i < N; i++) {
// Length of subsequence
// ending with arr[i]
m[arr[i]] = max(m[arr[i]],
m[arr[i] - 1] + 1);
// Update Answer
ans = max(ans, m[arr[i]]);
}
// Return Answer
return ans;
}
// Driver Code
int main()
{
int arr[] = { 3, 4, 2, 6, 5, 3, 7, 4, 5 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << longestSubsequence(arr, N);
return 0;
}
Java
// JAVA program of the above approach
import java.util.*;
class GFG
{
// Function to find the longest
// increasing consecutive subsequence
public static int longestSubsequence(int[] arr, int N)
{
// Stores the length of longest
// subsequence qnding with key
HashMap<Integer, Integer> m = new HashMap<>();
// Stores the required answer
int ans = 0;
// Loop to traverse array
for (int i = 0; i < N; i++) {
// Length of subsequence
// ending with arr[i]
if (m.containsKey(arr[i])) {
m.put(arr[i],
Math.max(m.get(arr[i]),
m.get(arr[i] - 1) + 1));
}
else {
m.put(arr[i], 1);
}
// Update Answer
ans = Math.max(ans, m.get(arr[i]));
}
// Return Answer
return ans;
}
// Driver Code
public static void main(String[] args)
{
int[] arr = new int[] { 3, 4, 2, 6, 5, 3, 7, 4, 5 };
int N = arr.length;
System.out.print(longestSubsequence(arr, N));
}
}
// This code is contributed by Taranpreet
Python
# Python program of the above approach
# Function to find the longest
# increasing consecutive subsequence
def longestSubsequence(arr, N):
# Stores the length of longest
# subsequence qnding with key
m = {}
# Stores the required answer
ans = 0
# Loop to traverse array
for i in range(0, N):
# Length of subsequence
# ending with arr[i]
if arr[i] in m and arr[i] - 1 in m:
m[arr[i]] = max(m[arr[i]], m[arr[i] - 1] + 1)
else:
m[arr[i]] = 1
# Update Answer
ans = max(ans, m[arr[i]])
# Return Answer
return ans
# Driver Code
arr = [3, 4, 2, 6, 5, 3, 7, 4, 5]
N = len(arr)
print(longestSubsequence(arr, N))
# This code is contributed by Samim Hossain Mondal.
C#
// C# program of the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to find the longest
// increasing consecutive subsequence
public static int longestSubsequence(int[] arr, int N)
{
// Stores the length of longest
// subsequence qnding with key
Dictionary<int, int> m = new Dictionary<int, int>();
// Stores the required answer
int ans = 0;
// Loop to traverse array
for (int i = 0; i < N; i++) {
// Length of subsequence
// ending with arr[i]
if (m.ContainsKey(arr[i])) {
m[arr[i]]=
Math.Max(m[arr[i]],
m[arr[i] - 1] + 1);
}
else {
m.Add(arr[i], 1);
}
// Update Answer
ans = Math.Max(ans, m[arr[i]]);
}
// Return Answer
return ans;
}
// Driver Code
public static void Main(String[] args)
{
int[] arr = new int[] { 3, 4, 2, 6, 5, 3, 7, 4, 5 };
int N = arr.Length;
Console.Write(longestSubsequence(arr, N));
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// JavaScript code for the above approach
// Function to find the longest
// increasing consecutive subsequence
function longestSubsequence(arr, N)
{
// Stores the length of longest
// subsequence qnding with key
let m = new Map();
// Stores the required answer
let ans = 0;
// Loop to traverse array
for (let i = 0; i < N; i++)
{
// Length of subsequence
// ending with arr[i]
if (m.has(arr[i]))
m.set(arr[i], Math.max(m.get(arr[i]),
m.get(arr[i] - 1) + 1));
else
m.set(arr[i], 1)
// Update Answer
ans = Math.max(ans, m.get(arr[i]));
}
// Return Answer
return ans;
}
// Driver Code
let arr = [3, 4, 2, 6, 5, 3, 7, 4, 5];
let N = arr.length;
document.write(longestSubsequence(arr, N));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Longest Increasing consecutive subsequence 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
10 min read
Printing longest Increasing consecutive subsequence Given n elements, write a program that prints the longest increasing subsequence whose adjacent element difference is one. Examples: Input : a[] = {3, 10, 3, 11, 4, 5, 6, 7, 8, 12} Output : 3 4 5 6 7 8 Explanation: 3, 4, 5, 6, 7, 8 is the longest increasing subsequence whose adjacent element differs
8 min read
Largest increasing subsequence of consecutive integers Given an array of n positive integers. We need to find the largest increasing sequence of consecutive positive integers. Examples: Input : arr[] = {5, 7, 6, 7, 8} Output : Size of LIS = 4 LIS = 5, 6, 7, 8 Input : arr[] = {5, 7, 8, 7, 5} Output : Size of LIS = 2 LIS = 7, 8 This problem can be solved
6 min read
Longest Increasing Odd Even Subsequence Given an array of size n. The problem is to find the length of the subsequence in the given array such that all the elements of the subsequence are sorted in increasing order and also they are alternately odd and even. Note that the subsequence could start either with the odd number or with the even
8 min read
Longest Consecutive Subsequence Given an array of integers, the task is to find the length of the longest subsequence such that elements in the subsequence are consecutive integers, the consecutive numbers can be in any order. Examples: Input: arr[] = [2, 6, 1, 9, 4, 5, 3]Output: 6Explanation: The consecutive numbers here are from
9 min read