title: Longest Subsequence Such That Difference Between Adjacents Is One
signature: longest_subsequence_such_that_difference_between_adjacents_is_one <- function(arr, n) {
source_py: def longest_subsequence_such_that_difference_between_adjacents_is_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
source_cpp: int longest_subsequence_such_that_difference_between_adjacents_is_one(vector<int> arr, int n) {
    int dp [ n ];
    for ( int i = 0;
    i < n;
    i ++ ) dp [ i ] = 1;
    for ( int i = 1;
    i < n;
    i ++ ) {
      for ( int j = 0;
      j < i;
      j ++ ) {
        if ( ( arr [ i ] == arr [ j ] + 1 ) || ( arr [ i ] == arr [ j ] - 1 ) ) dp [ i ] = max ( dp [ i ], dp [ j ] + 1 );
      }
    }
    int result = 1;
    for ( int i = 0;
    i < n;
    i ++ ) if ( result < dp [ i ] ) result = dp [ i ];
    return result;
  }