public static int maxLengthSub ( int [ ] arr ) {
  int max_val = 0 ;
  int start = 0 ;
  HashMap < Integer , Integer > map = new HashMap < > ( ) ;
  for ( int i = 0 ;
  i < arr . length ;
  i ++ ) {
    int temp = 0 ;
    if ( map . containsKey ( arr [ i ] - 1 ) ) {
      temp = map . get ( arr [ i ] - 1 ) ;
    }
    if ( map . containsKey ( arr [ i ] ) ) {
      temp = Math . max ( temp , map . get ( arr [ i ] ) ) ;
    }
    if ( map . containsKey ( arr [ i ] + 1 ) ) {
      temp = Math . max ( temp , map . get ( arr [ i ] + 1 ) ) ;
    }
    temp ++ ;
    if ( temp > max_val ) {
      max_val = temp ;
    }
    map . put ( arr [ i ] , temp ) ;
  }
  return max_val ;
}

public static int maxLengthSub ( int [ ] arr ) { int max_val = 0 ; int start = 0 ; HashMap < Integer , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { int temp = 0 ; if ( map . containsKey ( arr [ i ] - 1 ) ) { temp = map . get ( arr [ i ] - 1 ) ; } if ( map . containsKey ( arr [ i ] ) ) { temp = Math . max ( temp , map . get ( arr [ i ] ) ) ; } if ( map . containsKey ( arr [ i ] + 1 ) ) { temp = Math . max ( temp , map . get ( arr [ i ] + 1 ) ) ; } temp ++ ; if ( temp > max_val ) { max_val = temp ; } map . put ( arr [ i ] , temp ) ; } return max_val ; }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (integral_type) dimensions: (dimensions)) name: (identifier))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (generic_type (type_identifier) (type_arguments (type_identifier) (type_identifier))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments)) arguments: (argument_list)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (field_access object: (identifier) field: (identifier))) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (if_statement condition: (parenthesized_expression (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (decimal_integer_literal))))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (decimal_integer_literal)))))))) (if_statement condition: (parenthesized_expression (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (identifier) index: (identifier))))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (identifier) index: (identifier)))))))))) (if_statement condition: (parenthesized_expression (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (decimal_integer_literal))))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (decimal_integer_literal)))))))))) (expression_statement (update_expression (identifier))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (identifier))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (identifier) index: (identifier)) (identifier)))))) (return_statement (identifier))))