Problem Statement
You are organizing a tennis tournament. There are several players and they need to be paired up to participate. Each player has a level indicated by an integer value from 1 to 100. The level of a pair is the sum of the levels of both players. Of course, each player cannot be assigned to more than one pair. Also, for the tournament to be fun, you decided that all pairs needed to be of the same level. If necessary, some players may be rejected from the tournament. Of course, you want to minimize the number of rejected players.
You will be given the levels of all players as a int[]. Return the minimum number of players that need to be rejected.
Definition
- Class:
- TennisTournament
- Method:
- maxPairs
- Parameters:
- int[]
- Returns:
- int
- Method signature:
- int maxPairs(int[] level)
- (be sure your method is public)
Constraints
- level will contain between 1 and 50 elements, inclusive.
- Each element of level will be between 1 and 100, inclusive.
Examples
{1,2,4,5,7}Returns: 1
You can form two pairs of level 6 by taking (1,5) and (2,4).{1,1,1,3,3,2,2,2,2,2,2,2,2,2}Returns: 2
There are 6 pairs of level 4, two of the form (1,3) and 4 of the form (2,2). One player of level 1 and one of level 2 remain unpaired.{100}Returns: 1
With only one contestant, there is not much pairing you can do.{1,2,4,8,16,32,64}Returns: 5
The sum of any two distinct powers of 2 is different, so any possible pair has a different level. You must reject everybody except one pair.{1,2,3,4,5,6,7,8,9,10}Returns: 0
There are 5 pairs of level 11.