Given an array arr[] of size N and an integer K, the task is to find the longest subsequence such that the difference between adjacents is K.
Example:
Input: arr[]={1, 2, 3, 4, 5, 3, 2}, K=1
Output: 6
Explanation: The longest subsequence with the difference between the adjacent elements as 1 is: {1, 2, 3, 4, 3, 2}
Input: arr[]={1, 2, 3, 2, 3, 7, 2, 1}, K=2
Output: 3
Approach: The given problem can be solved using Dynamic Programming. The idea is to store the length of the longest subsequence having a difference K between adjacents ending after including the current element. Create an unordered map mp where mp[i] represents the maximum length of subsequence which includes integer i.
So, the relation to get the maximum length subsequence can be written as:
mp[i] = 1 + max(mp[i – K], mp[i + K])
The maximum integer stores in the map mp is the required answer. Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longestSubsequence(vector< int >& arr, int K)
{
unordered_map< int , int > mp;
int mx = 1;
for ( auto x : arr) {
mp[x] = 1;
if (mp.count(x - K)) {
mp[x] = 1 + mp[x - K];
}
if (mp.count(x + K)) {
mp[x] = max(mp[x], 1 + mp[x + K]);
}
mx = max(mx, mp[x]);
}
return mx;
}
int main()
{
vector< int > arr = { 1, 2, 3, 4, 5, 3, 2 };
int K = 1;
cout << longestSubsequence(arr, K);
}
|
Java
import java.util.HashMap;
class GFG {
public static int longestSubsequence( int [] arr, int K) {
HashMap<Integer, Integer> mp = new HashMap<Integer, Integer>();
int mx = 1 ;
for ( int x : arr) {
mp.put(x, 1 );
if (mp.containsKey(x - K)) {
mp.put(x, 1 + mp.get(x - K));
}
if (mp.containsKey(x + K)) {
mp.put(x, Math.max(mp.get(x), 1 + mp.get(x + K)));
}
mx = Math.max(mx, mp.get(x));
}
return mx;
}
public static void main(String args[]) {
int [] arr = { 1 , 2 , 3 , 4 , 5 , 3 , 2 };
int K = 1 ;
System.out.print(longestSubsequence(arr, K));
}
}
|
Python3
def longestSubsequence(arr, K):
mp = {}
mx = 1
for x in arr:
mp[x] = 1
if ((x - K) in mp):
mp[x] = 1 + mp[x - K]
if ((x + K) in mp):
mp[x] = max (mp[x], 1 + mp[x + K])
mx = max (mx, mp[x])
return mx
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 , 5 , 3 , 2 ]
K = 1
print (longestSubsequence(arr, K))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int longestSubsequence(List< int > arr, int K)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
int mx = 1;
foreach ( int x in arr)
{
mp[x] = 1;
if (mp.ContainsKey(x - K))
{
mp[x] = 1 + mp[x - K];
}
if (mp.ContainsKey(x + K))
{
mp[x] = Math.Max(mp[x], 1 + mp[x + K]);
}
mx = Math.Max(mx, mp[x]);
}
return mx;
}
public static void Main()
{
List< int > arr = new List< int >(){ 1, 2, 3, 4, 5, 3, 2 };
int K = 1;
Console.Write(longestSubsequence(arr, K));
}
}
|
Javascript
<script>
function longestSubsequence(arr, K)
{
let mp = new Map();
let mx = 1;
for (let x of arr) {
mp.set(x, 1)
if (mp.has(x - K)) {
mp.set(x, 1 + mp.get(x - K));
}
if (mp.has(x + K)) {
mp.set(x, 1 + mp.get(x + K));
}
mx = Math.max(mx, mp.get(x));
}
return mx;
}
let arr = [1, 2, 3, 4, 5, 3, 2];
let K = 1;
document.write(longestSubsequence(arr, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)