title: Maximum Length Subsequence Difference Adjacent Elements Either 0 1
signature: func maximumLengthSubsequenceDifferenceAdjacentElementsEither01(arr []int, n int) int {
test_code: package main
  
  import (
      "fmt"
      "reflect"
      "math"
      "sort"
  )
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  func validateSolution(actual int, expected int) bool {
      return reflect.DeepEqual(actual,expected)
  }
  
  func driver(arr []int, n int, expected int) string {
      if validateSolution(PLACEHOLDER_FN_NAME(arr, n),expected) {
              return "PASSED"
      }
      return "FAILED"
  
  }
  
  func main() {
  
      // Use imports to avoid undefined errors
      var _ = reflect.DeepEqual
      _ = math.Abs
      _ = sort.Sort
      result := ""
      result = driver([]int{4, 5, 9, 31, 31, 37, 41, 55, 56, 61, 79, 81, 89, 93}, 
  	12, 2)
      fmt.Printf("TEST-0...%s\n", result)
      
      result = driver([]int{-76, 96, -68, -16, 22, -24, -24, 6, 98, -82, 54, -80, 46, 0, 0, -50}, 
  	15, 2)
      fmt.Printf("TEST-1...%s\n", result)
      
      result = driver([]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
  	29, 29)
      fmt.Printf("TEST-2...%s\n", result)
      
      result = driver([]int{97, 93, 43, 51, 15, 33, 13, 96, 39, 89, 78, 25, 45, 6, 64, 54}, 
  	13, 2)
      fmt.Printf("TEST-3...%s\n", result)
      
      result = driver([]int{-98, -78, -72, -70, -68, -58, -56, -54, -46, -44, -38, -34, -30, -24, -18, -16, -14, -12, 2, 6, 8, 8, 10, 10, 16, 24, 26, 28, 40, 42, 44, 56, 58, 62, 66, 66, 78, 86}, 
  	19, 1)
      fmt.Printf("TEST-4...%s\n", result)
      
      result = driver([]int{1, 1, 0, 1, 0, 0}, 
  	3, 3)
      fmt.Printf("TEST-5...%s\n", result)
      
      result = driver([]int{7, 8, 10, 11, 12, 15, 16, 16, 19, 21, 23, 23, 23, 25, 26, 35, 38, 43, 46, 47, 51, 52, 53, 57, 60, 61, 66, 67, 69, 74, 75, 81, 83, 84, 88, 92, 94, 98, 98, 99}, 
  	34, 3)
      fmt.Printf("TEST-6...%s\n", result)
      
      result = driver([]int{14, -40}, 
  	1, 1)
      fmt.Printf("TEST-7...%s\n", result)
      
      result = driver([]int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, 
  	38, 38)
      fmt.Printf("TEST-8...%s\n", result)
      
      result = driver([]int{59, 70, 53}, 
  	1, 1)
      fmt.Printf("TEST-9...%s\n", result)
      
          
  }
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;
  }