Problem · Sorting
How Many Games Did the First Team Win?
Learn this problemProblem statement
Amazon Games is organizing a tournament of pair games. There are two groups, firstTeam and secondTeam, each containing n players. The skill level of the i-th player in each group is firstTeam[i] and secondTeam[i].
For each pair of indices (i, j) with 0 <= i < j < n, the pair (firstTeam[i], firstTeam[j]) competes against (secondTeam[i], secondTeam[j]). The first team wins that game if firstTeam[i] + firstTeam[j] > secondTeam[i] + secondTeam[j].
Return the number of games won by the first team, modulo 109 + 7.
Function
howManyGamesDidTheyWin(n: int, firstTeam: int[], secondTeam: int[]) → intExamples
Example 1
n = 3firstTeam = [1, 2, 3]secondTeam = [3, 2, 1]return = 1
The possible index pairs are (0, 1), (0, 2), and (1, 2).
(0, 1):1 + 2 = 3and3 + 2 = 5, so the first team loses.(0, 2):1 + 3 = 4and3 + 1 = 4, so the first team does not win.(1, 2):2 + 3 = 5and2 + 1 = 3, so the first team wins.
There is 1 winning game.
Constraints
2 <= n <= 105firstTeam.length == secondTeam.length == n1 <= firstTeam[i], secondTeam[i] <= 109