title: Longest Subsequence Such That Difference Between Adjacents Is One
signature: longest_subsequence_such_that_difference_between_adjacents_is_one <- function(arr, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(2L, 13L, 15L, 17L, 18L, 20L, 22L, 24L, 28L, 34L, 37L, 43L, 46L, 47L, 49L, 51L, 52L, 54L, 58L, 64L, 65L, 77L, 78L, 79L, 87L, 90L, 92L, 93L, 94L, 97L), 23L, 
          2L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-86L, -56L, 76L, 86L, 42L, -40L, 8L, 34L, -48L, -54L, -80L, 6L, 42L, 48L, 76L, -26L, 6L), 15L, 
          1L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 16L, 
          2L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(5L, 22L, 15L, 26L, 29L, 47L, 22L), 5L, 
          1L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-90L, -84L, -76L, -76L, -74L, -74L, -66L, -58L, -58L, -56L, -56L, -44L, -38L, -36L, -34L, -32L, -16L, -10L, 2L, 6L, 24L, 24L, 24L, 34L, 36L, 54L, 60L, 68L, 70L, 76L, 88L), 17L, 
          1L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L), 26L, 
          12L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(1L, 3L, 12L, 12L, 12L, 12L, 14L, 17L, 18L, 19L, 23L, 27L, 28L, 30L, 35L, 38L, 42L, 44L, 45L, 48L, 52L, 59L, 61L, 64L, 65L, 69L, 73L, 76L, 77L, 82L, 83L, 85L, 87L, 99L), 23L, 
          3L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-54L, -20L, 24L, -78L, -58L, 64L, 34L, 70L, -36L, -24L), 8L, 
          1L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 27L, 
          2L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(5L, 35L, 75L, 21L, 82L, 32L, 28L, 53L, 20L, 27L, 42L, 92L, 32L, 70L, 81L, 75L, 72L, 66L, 97L, 60L, 11L, 83L, 14L, 30L, 5L, 68L, 17L, 27L, 62L, 86L, 26L, 20L, 97L, 91L, 44L, 69L), 20L, 
          2L
      )))
  
  }
  
  main()
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;
  }