static int numOfWhiteHats ( int arr [ ] , int n ) {
  int freq [ ] = new int [ n + 1 ] ;
  Arrays . fill ( freq , 0 ) ;
  for ( int i = 0 ;
  i < n ;
  i ++ ) {
    if ( arr [ i ] >= n ) return - 1 ;
    freq [ arr [ i ] ] ++ ;
  }
  int diffFreq = 0 ;
  for ( int i = n - 1 ;
  i >= 0 ;
  i -- ) if ( freq [ i ] > 0 ) diffFreq ++ ;
  if ( diffFreq == 1 && freq [ n - 1 ] == n ) return n ;
  if ( diffFreq == 1 && freq [ 0 ] == n ) return 0 ;
  if ( diffFreq != 2 ) return - 1 ;
  int k ;
  for ( k = n - 1 ;
  k >= 1 ;
  k -- ) if ( freq [ k ] > 0 ) break ;
  if ( freq [ k - 1 ] == k && freq [ k ] + k == n ) return freq [ k - 1 ] ;
  else return - 1 ;
}

static int numOfWhiteHats ( int arr [ ] , int n ) { int freq [ ] = new int [ n + 1 ] ; Arrays . fill ( freq , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= n ) return - 1 ; freq [ arr [ i ] ] ++ ; } int diffFreq = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) if ( freq [ i ] > 0 ) diffFreq ++ ; if ( diffFreq == 1 && freq [ n - 1 ] == n ) return n ; if ( diffFreq == 1 && freq [ 0 ] == n ) return 0 ; if ( diffFreq != 2 ) return - 1 ; int k ; for ( k = n - 1 ; k >= 1 ; k -- ) if ( freq [ k ] > 0 ) break ; if ( freq [ k - 1 ] == k && freq [ k ] + k == n ) return freq [ k - 1 ] ; else return - 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) 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)))) (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: (array_access array: (identifier) index: (identifier)) right: (identifier))) consequence: (return_statement (unary_expression operand: (decimal_integer_literal)))) (expression_statement (update_expression (array_access array: (identifier) index: (array_access array: (identifier) index: (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: (binary_expression left: (identifier) right: (decimal_integer_literal)))) condition: (binary_expression left: (identifier) right: (decimal_integer_literal)) update: (update_expression (identifier)) body: (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (decimal_integer_literal))) consequence: (expression_statement (update_expression (identifier))))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (decimal_integer_literal)) right: (binary_expression left: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (identifier)))) consequence: (return_statement (identifier))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (decimal_integer_literal)) right: (binary_expression left: (array_access array: (identifier) index: (decimal_integer_literal)) right: (identifier)))) consequence: (return_statement (decimal_integer_literal))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (return_statement (unary_expression operand: (decimal_integer_literal)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier))) (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: (if_statement condition: (parenthesized_expression (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (decimal_integer_literal))) consequence: (break_statement))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (identifier)) right: (binary_expression left: (binary_expression left: (array_access array: (identifier) index: (identifier)) right: (identifier)) right: (identifier)))) consequence: (return_statement (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal)))) alternative: (return_statement (unary_expression operand: (decimal_integer_literal))))))