static int maxIndexDiff ( int arr [ ] , int n ) {
  int maxDiff ;
  int i , j ;
  int LMin [ ] , RMax [ ] ;
  LMin = new int [ n ] ;
  RMax = new int [ n ] ;
  LMin [ 0 ] = arr [ 0 ] ;
  for ( i = 1 ;
  i < n ;
  ++ i ) LMin [ i ] = Math . min ( arr [ i ] , LMin [ i - 1 ] ) ;
  RMax [ n - 1 ] = arr [ n - 1 ] ;
  for ( j = n - 2 ;
  j >= 0 ;
  -- j ) RMax [ j ] = Math . max ( arr [ j ] , RMax [ j + 1 ] ) ;
  i = 0 ;
  j = 0 ;
  maxDiff = - 1 ;
  while ( j < n && i < n ) {
    if ( LMin [ i ] < RMax [ j ] ) {
      maxDiff = Math . max ( maxDiff , j - i ) ;
      j = j + 1 ;
    }
    else i = i + 1 ;
  }
  return ( maxDiff + 1 ) ;
}

static int maxIndexDiff ( int arr [ ] , int n ) { int maxDiff ; int i , j ; int LMin [ ] , RMax [ ] ; LMin = new int [ n ] ; RMax = new int [ n ] ; LMin [ 0 ] = arr [ 0 ] ; for ( i = 1 ; i < n ; ++ i ) LMin [ i ] = Math . min ( arr [ i ] , LMin [ i - 1 ] ) ; RMax [ n - 1 ] = arr [ n - 1 ] ; for ( j = n - 2 ; j >= 0 ; -- j ) RMax [ j ] = Math . max ( arr [ j ] , RMax [ j + 1 ] ) ; i = 0 ; j = 0 ; maxDiff = - 1 ; while ( j < n && i < n ) { if ( LMin [ i ] < RMax [ j ] ) { maxDiff = Math . max ( maxDiff , j - i ) ; j = j + 1 ; } else i = i + 1 ; } return ( maxDiff + 1 ) ; }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (integral_type) name: (identifier) dimensions: (dimensions)) (formal_parameter type: (integral_type) name: (identifier))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier)) declarator: (variable_declarator name: (identifier))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) dimensions: (dimensions))) (expression_statement (assignment_expression left: (identifier) right: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier))))) (expression_statement (assignment_expression left: (identifier) right: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier))))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (decimal_integer_literal)) right: (array_access array: (identifier) index: (decimal_integer_literal)))) (for_statement init: (assignment_expression left: (identifier) right: (decimal_integer_literal)) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (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: (binary_expression left: (identifier) right: (decimal_integer_literal)))))))) (expression_statement (assignment_expression left: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))) (for_statement init: (assignment_expression left: (identifier) right: (binary_expression left: (identifier) right: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (decimal_integer_literal)) update: (update_expression (identifier)) body: (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: (binary_expression left: (identifier) right: (decimal_integer_literal)))))))) (expression_statement (assignment_expression left: (identifier) right: (decimal_integer_literal))) (expression_statement (assignment_expression left: (identifier) right: (decimal_integer_literal))) (expression_statement (assignment_expression left: (identifier) right: (unary_expression operand: (decimal_integer_literal)))) (while_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (identifier) right: (identifier)))) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (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) (binary_expression left: (identifier) right: (identifier)))))) (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (identifier) right: (decimal_integer_literal))))) alternative: (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (identifier) right: (decimal_integer_literal))))))) (return_statement (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))))))