WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Dynamic Programming · Expert · question 78 of 100

Solve the problem of finding the Longest Consecutive Subsequence with Absolute Difference at Most K using dynamic programming.?

📕 Buy this interview preparation book: 100 Dynamic Programming questions & answers — PDF + EPUB for $5

The problem statement is to find the length of the longest subsequence where the absolute difference between any two consecutive elements in the subsequence is at most k. Here, let’s assume that the input array is ’arr’ and the absolute difference between elements is ’k’.

The brute force solution for this problem would be to generate all possible subsequences and check if each subsequence has the desired property. However, this would take an exponential time complexity of O(2n).

Instead of that, we can solve this problem using dynamic programming. Let’s create an array ’dp’ of size n to store the length of the longest subsequence ending at the ith position.

The dynamic programming approach is as follows:

1. Initialize the array dp[] = 1, 1, ., 1 since any element in the array of length 1 can be considered as a subsequence of length one.

2. Traverse the array and for each i, j <= i - 1, if |arr[j] - arr[i]| k, then update dp[i] as maximum(dp[i], dp[j] + 1).

3. Return the maximum value among all dp[i].

The above approach works because we maintain the longest subsequence ending at any position in the input array. For any index i, we iterate over all indices (0 to i-1) and check if the absolute difference between arr[i] and arr[j] is at most k. If it is, then we add 1 to the length of the longest subsequence ending at arr[j], which becomes the length of the longest subsequence ending at arr[i] if it’s greater than the value of dp[i] calculated earlier.

Let’s illustrate this algorithm by taking an example.

Example:

Input array: [8, 4, 7, 5, 1, 2, 6, 3]
k = 3

We need to find the length of the longest subsequence with absolute difference at most k.

Apply the above dynamic programming approach, we get:

dp[] = [1, 1, 2, 2, 1, 2, 3, 2]

The final answer is 3 since the maximum value in dp[] is 3.

Java Implementation:

public int longestConsecutiveSubsequence(int[] arr, int k) {
    int n = arr.length;
    int[] dp = new int[n];
    Arrays.fill(dp, 1);
    int maxLen = 1;
    for (int i = 1; i < n; i++) {
        for (int j = 0; j < i; j++) {
            if (Math.abs(arr[i] - arr[j]) <= k) {
                dp[i] = Math.max(dp[i], dp[j] + 1);
            }
        }
        maxLen = Math.max(maxLen, dp[i]);
    }
    return maxLen;
}

Time Complexity: O(n2) Space Complexity: O(n)

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Dynamic Programming interview — then scores it.
📞 Practice Dynamic Programming — free 15 min
📕 Buy this interview preparation book: 100 Dynamic Programming questions & answers — PDF + EPUB for $5

All 100 Dynamic Programming questions · All topics