blob: d2be437d4a95d21f4eeb32bdce7bb76b8a40c258 (
plain)
1
2
3
4
5
6
7
8
9
10
11
|
def longest_subseq_with_diff_one(arr, n):
dp = [1 for i in range(n)]
for i in range(n):
for j in range(i):
if ((arr[i] == arr[j]+1) or (arr[i] == arr[j]-1)):
dp[i] = max(dp[i], dp[j]+1)
result = 1
for i in range(n):
if (result < dp[i]):
result = dp[i]
return result
|