static int maxModValue ( int arr [ ] , int n ) {
  int ans = 0 ;
  Arrays . sort ( arr ) ;
  for ( int j = n - 2 ;
  j >= 0 ;
  -- j ) {
    if ( ans >= arr [ j ] ) break ;
    if ( arr [ j ] == arr [ j + 1 ] ) continue ;
    for ( int i = 2 * arr [ j ] ;
    i <= arr [ n - 1 ] + arr [ j ] ;
    i += arr [ j ] ) {
      int ind = Arrays . binarySearch ( arr , i ) ;
      if ( ind < 0 ) ind = Math . abs ( ind + 1 ) ;
      else {
        while ( arr [ ind ] == i ) {
          ind -- ;
          if ( ind == 0 ) {
            ind = - 1 ;
            break ;
          }
        }
        ind ++ ;
      }
      ans = Math . max ( ans , arr [ ind - 1 ] % arr [ j ] ) ;
    }
  }
  return ans ;
}

static int maxModValue ( int arr [ ] , int n ) { int ans = 0 ; Arrays . sort ( arr ) ; for ( int j = n - 2 ; j >= 0 ; -- j ) { if ( ans >= arr [ j ] ) break ; if ( arr [ j ] == arr [ j + 1 ] ) continue ; for ( int i = 2 * arr [ j ] ; i <= arr [ n - 1 ] + arr [ j ] ; i += arr [ j ] ) { int ind = Arrays . binarySearch ( arr , i ) ; if ( ind < 0 ) ind = Math . abs ( ind + 1 ) ; else { while ( arr [ ind ] == i ) { ind -- ; if ( ind == 0 ) { ind = - 1 ; break ; } } ind ++ ; } ans = Math . max ( ans , arr [ ind - 1 ] % arr [ j ] ) ; } } return ans ; }
(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) value: (decimal_integer_literal))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))) (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 (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (array_access array: (identifier) index: (identifier)))) consequence: (break_statement)) (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))) consequence: (continue_statement)) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (decimal_integer_literal) right: (array_access array: (identifier) index: (identifier))))) condition: (binary_expression left: (identifier) right: (binary_expression left: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (array_access array: (identifier) index: (identifier)))) update: (assignment_expression left: (identifier) right: (array_access array: (identifier) index: (identifier))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (identifier))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (expression_statement (assignment_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)))))) alternative: (block (while_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (identifier))) body: (block (expression_statement (update_expression (identifier))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (unary_expression operand: (decimal_integer_literal)))) (break_statement))))) (expression_statement (update_expression (identifier))))) (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: (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (array_access array: (identifier) index: (identifier))))))))))) (return_statement (identifier))))