title: Maximum Length Subsequence Difference Adjacent Elements Either 0 1
signature: class Solution {
  
      public Integer maximumLengthSubsequenceDifferenceAdjacentElementsEither01(ArrayList<Integer> arr, Integer n) {
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;
  }
question_info.extension: java
question_info.entry_fn_name: maximumLengthSubsequenceDifferenceAdjacentElementsEither01
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 2, "inputs": {"arr": [4, 5, 9, 31, 31, 37, 41, 55, 56, 61, 79, 81, 89, 93], "n": 12}}, {"idx": 1, "outputs": 2, "inputs": {"arr": [-76, 96, -68, -16, 22, -24, -24, 6, 98, -82, 54, -80, 46, 0, 0, -50], "n": 15}}, {"idx": 2, "outputs": 29, "inputs": {"arr": [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], "n": 29}}, {"idx": 3, "outputs": 2, "inputs": {"arr": [97, 93, 43, 51, 15, 33, 13, 96, 39, 89, 78, 25, 45, 6, 64, 54], "n": 13}}, {"idx": 4, "outputs": 1, "inputs": {"arr": [-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], "n": 19}}, {"idx": 5, "outputs": 3, "inputs": {"arr": [1, 1, 0, 1, 0, 0], "n": 3}}, {"idx": 6, "outputs": 3, "inputs": {"arr": [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], "n": 34}}, {"idx": 7, "outputs": 1, "inputs": {"arr": [14, -40], "n": 1}}, {"idx": 8, "outputs": 38, "inputs": {"arr": [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], "n": 38}}, {"idx": 9, "outputs": 1, "inputs": {"arr": [59, 70, 53], "n": 1}}]
question_info.test_code: import static java.util.Map.entry;  
  import java.util.*;
  import java.lang.*;
  // TESTING CODE 
  // ============================================
  class QuestionEvaluator {
      public static String validateSolution(ArrayList<Integer> arr, Integer n, Integer expected){
          try{
              Driver driver = new Driver();
              if (driver.run(arr, n,expected)){
                  return "PASSED";
              }
              return "FAILED";
              
          } catch (Exception exception_obj){
              return exception_obj.getClass().getSimpleName();
          }
  
      }
      public static void main(String[] args) {
          String result;
          result = validateSolution(new ArrayList<>(Arrays.asList(4, 5, 9, 31, 31, 37, 41, 55, 56, 61, 79, 81, 89, 93)), 12, 2);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-76, 96, -68, -16, 22, -24, -24, 6, 98, -82, 54, -80, 46, 0, 0, -50)), 15, 2);
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(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);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(97, 93, 43, 51, 15, 33, 13, 96, 39, 89, 78, 25, 45, 6, 64, 54)), 13, 2);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-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);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(1, 1, 0, 1, 0, 0)), 3, 3);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(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);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(14, -40)), 1, 1);
          System.out.println(String.format("%s%s","TEST-7...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(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);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(59, 70, 53)), 1, 1);
          System.out.println(String.format("%s%s","TEST-9...",result));    
      
      }
  }
  
  class Driver{
      public boolean run(ArrayList<Integer> arr, Integer n, Integer expected){
  
          PLACEHOLDER_CLS_NAME sol = new PLACEHOLDER_CLS_NAME();
          return validateSolution(sol.PLACEHOLDER_FN_NAME(arr, n),expected);
      }
      
      public boolean validateSolution(Integer actual, Integer expected){
          return actual.equals(expected);
      }
  }
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: java __FILENAME__