static int countPairs ( int arr [ ] , int n ) {
  int forward [ ] = new int [ n ] ;
  Stack < Integer > sForward = new Stack < Integer > ( ) ;
  for ( int i = 0 ;
  i < n ;
  i ++ ) {
    while ( ! sForward . empty ( ) && arr [ i ] > arr [ ( Integer ) sForward . peek ( ) ] ) {
      forward [ ( Integer ) sForward . peek ( ) ] = 1 ;
      sForward . pop ( ) ;
    }
    sForward . push ( i ) ;
  }
  int backward [ ] = new int [ n ] ;
  Stack < Integer > sBackward = new Stack < Integer > ( ) ;
  for ( int i = n - 1 ;
  i >= 0 ;
  i -- ) {
    while ( ! sBackward . empty ( ) && arr [ i ] > arr [ ( Integer ) sBackward . peek ( ) ] ) {
      backward [ ( Integer ) sBackward . peek ( ) ] = 1 ;
      sBackward . pop ( ) ;
    }
    sBackward . push ( i ) ;
  }
  int res = 0 ;
  for ( int i = 0 ;
  i < n ;
  i ++ ) {
    res += forward [ i ] + backward [ i ] ;
  }
  return res ;
}

static int countPairs ( int arr [ ] , int n ) { int forward [ ] = new int [ n ] ; Stack < Integer > sForward = new Stack < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( ! sForward . empty ( ) && arr [ i ] > arr [ ( Integer ) sForward . peek ( ) ] ) { forward [ ( Integer ) sForward . peek ( ) ] = 1 ; sForward . pop ( ) ; } sForward . push ( i ) ; } int backward [ ] = new int [ n ] ; Stack < Integer > sBackward = new Stack < Integer > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( ! sBackward . empty ( ) && arr [ i ] > arr [ ( Integer ) sBackward . peek ( ) ] ) { backward [ ( Integer ) sBackward . peek ( ) ] = 1 ; sBackward . pop ( ) ; } sBackward . push ( i ) ; } int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { res += forward [ i ] + backward [ i ] ; } return res ; }
(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) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier))))) (local_variable_declaration type: (generic_type (type_identifier) (type_arguments (type_identifier))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments (type_identifier))) 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: (identifier)) update: (update_expression (identifier)) body: (block (while_statement condition: (parenthesized_expression (binary_expression left: (unary_expression operand: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) right: (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (cast_expression type: (type_identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))))))) body: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (cast_expression type: (type_identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) right: (decimal_integer_literal))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier))))) (local_variable_declaration type: (generic_type (type_identifier) (type_arguments (type_identifier))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments (type_identifier))) arguments: (argument_list)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (identifier) right: (decimal_integer_literal)))) condition: (binary_expression left: (identifier) right: (decimal_integer_literal)) update: (update_expression (identifier)) body: (block (while_statement condition: (parenthesized_expression (binary_expression left: (unary_expression operand: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) right: (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (cast_expression type: (type_identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))))))) body: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (cast_expression type: (type_identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) right: (decimal_integer_literal))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (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: (identifier)) update: (update_expression (identifier)) body: (block (expression_statement (assignment_expression left: (identifier) right: (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (identifier))))))) (return_statement (identifier))))