title: Maximum Length Subsequence Difference Adjacent Elements Either 0 1
signature: maximum_length_subsequence_difference_adjacent_elements_either_0_1 <- 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(4L, 5L, 9L, 31L, 31L, 37L, 41L, 55L, 56L, 61L, 79L, 81L, 89L, 93L), 12L, 
          2L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-76L, 96L, -68L, -16L, 22L, -24L, -24L, 6L, 98L, -82L, 54L, -80L, 46L, 0L, 0L, -50L), 15L, 
          2L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 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), 29L, 
          29L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(97L, 93L, 43L, 51L, 15L, 33L, 13L, 96L, 39L, 89L, 78L, 25L, 45L, 6L, 64L, 54L), 13L, 
          2L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-98L, -78L, -72L, -70L, -68L, -58L, -56L, -54L, -46L, -44L, -38L, -34L, -30L, -24L, -18L, -16L, -14L, -12L, 2L, 6L, 8L, 8L, 10L, 10L, 16L, 24L, 26L, 28L, 40L, 42L, 44L, 56L, 58L, 62L, 66L, 66L, 78L, 86L), 19L, 
          1L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 1L, 0L, 1L, 0L, 0L), 3L, 
          3L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(7L, 8L, 10L, 11L, 12L, 15L, 16L, 16L, 19L, 21L, 23L, 23L, 23L, 25L, 26L, 35L, 38L, 43L, 46L, 47L, 51L, 52L, 53L, 57L, 60L, 61L, 66L, 67L, 69L, 74L, 75L, 81L, 83L, 84L, 88L, 92L, 94L, 98L, 98L, 99L), 34L, 
          3L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(14L, -40L), 1L, 
          1L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 38L, 
          38L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(59L, 70L, 53L), 1L, 
          1L
      )))
  
  }
  
  main()
source_py: def maximum_length_subsequence_difference_adjacent_elements_either_0_1(arr, n):
      mls = []
      max = 0
      for i in range(n):
          mls.append(1)
      for i in range(n):
          for j in range(i):
              if ((abs((arr[i] - arr[j])) <= 1) and (mls[i] < (mls[j] + 1))):
                  mls[i] = (mls[j] + 1)
      for i in range(n):
          if (max < mls[i]):
              max = mls[i]
      return max
source_cpp: int maximum_length_subsequence_difference_adjacent_elements_either_0_1(vector<int> arr, int n) {
    int mls [ n ], max = 0;
    for ( int i = 0;
    i < n;
    i ++ ) mls [ i ] = 1;
    for ( int i = 1;
    i < n;
    i ++ ) for ( int j = 0;
    j < i;
    j ++ ) if ( abs ( arr [ i ] - arr [ j ] ) <= 1 && mls [ i ] < mls [ j ] + 1 ) mls [ i ] = mls [ j ] + 1;
    for ( int i = 0;
    i < n;
    i ++ ) if ( max < mls [ i ] ) max = mls [ i ];
    return max;
  }