Open In App

Longest Increasing consecutive subsequence | Set-2

Last Updated : 24 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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:
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:

 

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>

Output
4

Time Complexity: O(N)
Auxiliary Space: O(N)


Next Article
Article Tags :
Practice Tags :

Similar Reads