There are n people on a social media site. You are given an integer array ages, where ages[i] is the age of person i.
Person x does not send a friend request to person y (x != y) if any of the following conditions is true:
-
ages[y] <= 0.5 * ages[x] + 7
-
ages[y] > ages[x]
-
ages[y] > 100 and ages[x] < 100
Otherwise, x sends a friend request to y.
If x sends a request to y, y does not necessarily send one to x. A person never sends a friend request to themselves.
Return the total number of friend requests sent.
Function Signature
def num_friend_requests(ages: list[int]) -> int:
Rules
-
Requests are directed. The answer is the number of ordered pairs
(x, y)
of different people for which none of the three conditions is true.
-
Two people with the same age are still different people, so each of them can send a request to the other.
Constraints
-
n == len(ages)
-
1 <= n <= 2 * 10^4
-
1 <= ages[i] <= 120
Examples
Input: ages = [16, 16]
Output: 2
For each person, the other one is 16, which is greater than 0.5 * 16 + 7 = 15 and not older. Each of the two sends one request.
Input: ages = [16, 17, 18]
Output: 2
The 17-year-old sends to the 16-year-old (16 > 15.5), and the 18-year-old sends to the 17-year-old (17 > 16). The 18-year-old does not send to the 16-year-old because 16 is not greater than 0.5 * 18 + 7 = 16, and nobody sends to someone older.
Input: ages = [20, 30, 100, 110, 120]
Output: 3
The 110-year-old sends to the 100-year-old, and the 120-year-old sends to both the 100-year-old and the 110-year-old. No one else has a younger person above their cutoff: the 100-year-old, for example, could only request someone older than 57.