Show problems as
Classic problem #2491MediumArrayHash TableTwo PointersSorting

Divide Players Into Teams of Equal Skill

Real-world version: Balanced Sculling Boats
Tracked in this browser only
Read Divide Players Into Teams of Equal Skill on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A rowing club owns two-seat sculling boats and has an even number of rowers on the water. Every rower must be seated in exactly one boat, and every boat must be full.

You are given skill, where skill[i] is the rating of rower i. For a crewed boat, its balance is the total of the two ratings aboard and its drive is the product of those two ratings.

The coach insists that all the boats share the same balance. Return the total drive across every boat when the rowers can be seated that way. When no seating gives every boat the same balance, return -1.

Ratings are at least 1, so a genuine total drive is always positive and can never be confused with the -1 answer.

Examples

Example 1

Input
skill = [3, 4, 6, 5]
Output
38

Seating 3 with 6 and 4 with 5 gives both boats a balance of 9. Their drives are 18 and 20, so the total drive is 38.

Example 2

Input
skill = [1, 2, 3, 5]
Output
-1

Two boats are needed. The three ways to seat these four rowers give balance pairs of 3 and 8, 4 and 7, then 6 and 5, and no pair is equal, so the answer is -1.

Example 3

Input
skill = [1000, 1, 1, 1000]
Output
2000

Each boat takes one rating of 1 and one of 1000, so both balances are 1001 and both drives are 1000, for a total of 2000.

Constraints

  • 2 <= skill.length <= 10^5
  • skill.length is even.
  • 1 <= skill[i] <= 1000
  • The total drive never exceeds 5 * 10^10, so it fits comfortably in a 64-bit integer.

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def divide_players(skill: list[int]) -> int:
Java
public long dividePlayers(int[] skill)