static int maxSortedAdjacentDiff ( int [ ] arr , int n ) {
  int maxVal = arr [ 0 ] ;
  int minVal = arr [ 0 ] ;
  for ( int i = 1 ;
  i < n ;
  i ++ ) {
    maxVal = Math . max ( maxVal , arr [ i ] ) ;
    minVal = Math . min ( minVal , arr [ i ] ) ;
  }
  int maxBucket [ ] = new int [ n - 1 ] ;
  int minBucket [ ] = new int [ n - 1 ] ;
  Arrays . fill ( maxBucket , 0 , n - 1 , Integer . MIN_VALUE ) ;
  Arrays . fill ( minBucket , 0 , n - 1 , Integer . MAX_VALUE ) ;
  float delta = ( float ) ( maxVal - minVal ) / ( float ) ( n - 1 ) ;
  for ( int i = 0 ;
  i < n ;
  i ++ ) {
    if ( arr [ i ] == maxVal || arr [ i ] == minVal ) {
      continue ;
    }
    int index = ( int ) ( Math . round ( ( arr [ i ] - minVal ) / delta ) ) ;
    if ( maxBucket [ index ] == Integer . MIN_VALUE ) {
      maxBucket [ index ] = arr [ i ] ;
    }
    else {
      maxBucket [ index ] = Math . max ( maxBucket [ index ] , arr [ i ] ) ;
    }
    if ( minBucket [ index ] == Integer . MAX_VALUE ) {
      minBucket [ index ] = arr [ i ] ;
    }
    else {
      minBucket [ index ] = Math . min ( minBucket [ index ] , arr [ i ] ) ;
    }
  }
  int prev_val = minVal ;
  int max_gap = 0 ;
  for ( int i = 0 ;
  i < n - 1 ;
  i ++ ) {
    if ( minBucket [ i ] == Integer . MAX_VALUE ) {
      continue ;
    }
    max_gap = Math . max ( max_gap , minBucket [ i ] - prev_val ) ;
    prev_val = maxBucket [ i ] ;
  }
  max_gap = Math . max ( max_gap , maxVal - prev_val ) ;
  return max_gap ;
}

static int maxSortedAdjacentDiff ( int [ ] arr , int n ) { int maxVal = arr [ 0 ] ; int minVal = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { maxVal = Math . max ( maxVal , arr [ i ] ) ; minVal = Math . min ( minVal , arr [ i ] ) ; } int maxBucket [ ] = new int [ n - 1 ] ; int minBucket [ ] = new int [ n - 1 ] ; Arrays . fill ( maxBucket , 0 , n - 1 , Integer . MIN_VALUE ) ; Arrays . fill ( minBucket , 0 , n - 1 , Integer . MAX_VALUE ) ; float delta = ( float ) ( maxVal - minVal ) / ( float ) ( n - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == maxVal || arr [ i ] == minVal ) { continue ; } int index = ( int ) ( Math . round ( ( arr [ i ] - minVal ) / delta ) ) ; if ( maxBucket [ index ] == Integer . MIN_VALUE ) { maxBucket [ index ] = arr [ i ] ; } else { maxBucket [ index ] = Math . max ( maxBucket [ index ] , arr [ i ] ) ; } if ( minBucket [ index ] == Integer . MAX_VALUE ) { minBucket [ index ] = arr [ i ] ; } else { minBucket [ index ] = Math . min ( minBucket [ index ] , arr [ i ] ) ; } } int prev_val = minVal ; int max_gap = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( minBucket [ i ] == Integer . MAX_VALUE ) { continue ; } max_gap = Math . max ( max_gap , minBucket [ i ] - prev_val ) ; prev_val = maxBucket [ i ] ; } max_gap = Math . max ( max_gap , maxVal - prev_val ) ; return max_gap ; }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (integral_type) dimensions: (dimensions)) name: (identifier)) (formal_parameter type: (integral_type) name: (identifier))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (array_access array: (identifier) index: (decimal_integer_literal)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (array_access array: (identifier) index: (decimal_integer_literal)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (array_access array: (identifier) index: (identifier)))))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (array_access array: (identifier) index: (identifier)))))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (binary_expression left: (identifier) right: (decimal_integer_literal)))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (binary_expression left: (identifier) right: (decimal_integer_literal)))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (decimal_integer_literal) (binary_expression left: (identifier) right: (decimal_integer_literal)) (field_access object: (identifier) field: (identifier))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (decimal_integer_literal) (binary_expression left: (identifier) right: (decimal_integer_literal)) (field_access object: (identifier) field: (identifier))))) (local_variable_declaration type: (floating_point_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (cast_expression type: (floating_point_type) value: (parenthesized_expression (binary_expression left: (identifier) right: (identifier)))) right: (cast_expression type: (floating_point_type) value: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (identifier)) right: (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (identifier)))) consequence: (block (continue_statement))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (cast_expression type: (integral_type) value: (parenthesized_expression (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (identifier))) right: (identifier)))))))) (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (field_access object: (identifier) field: (identifier)))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (identifier))))) alternative: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (identifier) index: (identifier)) (array_access array: (identifier) index: (identifier)))))))) (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (field_access object: (identifier) field: (identifier)))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (identifier))))) alternative: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (array_access array: (identifier) index: (identifier)) (array_access array: (identifier) index: (identifier)))))))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (identifier))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (binary_expression left: (identifier) right: (decimal_integer_literal))) update: (update_expression (identifier)) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (field_access object: (identifier) field: (identifier)))) consequence: (block (continue_statement))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (identifier)))))) (expression_statement (assignment_expression left: (identifier) right: (array_access array: (identifier) index: (identifier)))))) (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (identifier) right: (identifier)))))) (return_statement (identifier))))