The Wayback Machine - https://web.archive.org/web/20211019102656/https://www.geeksforgeeks.org/longest-non-decreasing-subsequence-having-difference-between-adjacent-elements-less-than-d/
Skip to content
Related Articles

Related Articles

Improve Article

Longest non-decreasing subsequence having difference between adjacent elements less than D

  • Last Updated : 19 Oct, 2021
Geek Week

Given an array arr[] of N integers and an integer D, the task is to find the length of the longest non-decreasing subsequence such that the difference between every adjacent element is less than D.

Examples:

Input: arr[] = {1, 3, 2, 4, 5}, D = 2
Output: 3
Explanation:
Consider the subsequence as {3, 4, 5}, which is of maximum length = 3 satisfying the given criteria.

Input: arr[] = {1, 5, 3, 2, 7}, D = 2
Output: 2

Approach: The given problem is a variation of Longest Increasing Subsequence with criteria for the difference between adjacent array elements as less than D, this idea can be implemented using Dynamic Programming. Follow the steps below to solve the given problem:



  • Initialize a dp array, where dp[i] will store the maximum length of non-decreasing subsequence after including the ith element such that the difference between every adjacent pair of elements is less than D.
  • Initialize all values of the array dp[] as 1.
  • Iterate a loop over the range [0, N] and in each iteration, i traverse the given array arr[] over the range [0, i – 1] using the variable j and if the value of arr[j] is at least arr[i] and the difference between them is less than D, then update the value of dp[i] to the maximum of dp[i] and (1 + dp[j]).
  • After completing the above steps, print the maximum value of the array dp[] as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the length of the
// longest non-decreasing subsequence
// having the difference as D for every
// adjacent elements
int longestSubsequence(vector<int> arr,
                       int d)
{
    // Store the size of array
    int n = arr.size();
  
    // Stores the maximum length of the
    // subsequence after including the
    // ith element
    vector<int> dp(n, 1);
  
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) {
  
            // If it satisfies the
            // given condition
            if (arr[i] - d < arr[j]
                and arr[i] >= arr[j]) {
  
                // Update dp[i]
                dp[i] = max(dp[i], dp[j] + 1);
            }
        }
    }
  
    // Maximum value in the dp
    // table is the answer
    return *max_element(
        dp.begin(), dp.end());
}
  
// Driver Code
int main()
{
    vector<int> arr = { 1, 3, 2, 4, 5 };
    int D = 2;
    cout << longestSubsequence(arr, D);
  
    return 0;
}
Output:
3

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :