title: Longest Subsequence Such That Difference Between Adjacents Is One
signature: class Solution {
  
      public Integer longestSubsequenceSuchThatDifferenceBetweenAdjacentsIsOne(ArrayList<Integer> arr, Integer 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;
  }
question_info.extension: java
question_info.entry_fn_name: longestSubsequenceSuchThatDifferenceBetweenAdjacentsIsOne
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 2, "inputs": {"arr": [2, 13, 15, 17, 18, 20, 22, 24, 28, 34, 37, 43, 46, 47, 49, 51, 52, 54, 58, 64, 65, 77, 78, 79, 87, 90, 92, 93, 94, 97], "n": 23}}, {"idx": 1, "outputs": 1, "inputs": {"arr": [-86, -56, 76, 86, 42, -40, 8, 34, -48, -54, -80, 6, 42, 48, 76, -26, 6], "n": 15}}, {"idx": 2, "outputs": 2, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 16}}, {"idx": 3, "outputs": 1, "inputs": {"arr": [5, 22, 15, 26, 29, 47, 22], "n": 5}}, {"idx": 4, "outputs": 1, "inputs": {"arr": [-90, -84, -76, -76, -74, -74, -66, -58, -58, -56, -56, -44, -38, -36, -34, -32, -16, -10, 2, 6, 24, 24, 24, 34, 36, 54, 60, 68, 70, 76, 88], "n": 17}}, {"idx": 5, "outputs": 12, "inputs": {"arr": [0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1], "n": 26}}, {"idx": 6, "outputs": 3, "inputs": {"arr": [1, 3, 12, 12, 12, 12, 14, 17, 18, 19, 23, 27, 28, 30, 35, 38, 42, 44, 45, 48, 52, 59, 61, 64, 65, 69, 73, 76, 77, 82, 83, 85, 87, 99], "n": 23}}, {"idx": 7, "outputs": 1, "inputs": {"arr": [-54, -20, 24, -78, -58, 64, 34, 70, -36, -24], "n": 8}}, {"idx": 8, "outputs": 2, "inputs": {"arr": [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, 1, 1, 1], "n": 27}}, {"idx": 9, "outputs": 2, "inputs": {"arr": [5, 35, 75, 21, 82, 32, 28, 53, 20, 27, 42, 92, 32, 70, 81, 75, 72, 66, 97, 60, 11, 83, 14, 30, 5, 68, 17, 27, 62, 86, 26, 20, 97, 91, 44, 69], "n": 20}}]
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(2, 13, 15, 17, 18, 20, 22, 24, 28, 34, 37, 43, 46, 47, 49, 51, 52, 54, 58, 64, 65, 77, 78, 79, 87, 90, 92, 93, 94, 97)), 23, 2);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-86, -56, 76, 86, 42, -40, 8, 34, -48, -54, -80, 6, 42, 48, 76, -26, 6)), 15, 1);
          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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 16, 2);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(5, 22, 15, 26, 29, 47, 22)), 5, 1);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-90, -84, -76, -76, -74, -74, -66, -58, -58, -56, -56, -44, -38, -36, -34, -32, -16, -10, 2, 6, 24, 24, 24, 34, 36, 54, 60, 68, 70, 76, 88)), 17, 1);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1)), 26, 12);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(1, 3, 12, 12, 12, 12, 14, 17, 18, 19, 23, 27, 28, 30, 35, 38, 42, 44, 45, 48, 52, 59, 61, 64, 65, 69, 73, 76, 77, 82, 83, 85, 87, 99)), 23, 3);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-54, -20, 24, -78, -58, 64, 34, 70, -36, -24)), 8, 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, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 27, 2);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(5, 35, 75, 21, 82, 32, 28, 53, 20, 27, 42, 92, 32, 70, 81, 75, 72, 66, 97, 60, 11, 83, 14, 30, 5, 68, 17, 27, 62, 86, 26, 20, 97, 91, 44, 69)), 20, 2);
          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__